【问题标题】:In ASP.NET encoded quotes prevent request from reaching the route在 ASP.NET 编码的引号中阻止请求到达路由
【发布时间】:2020-04-16 18:46:56
【问题描述】:

在 ASP.NET 中编码的引号阻止请求到达路由。我该如何解决这个问题?

就我而言,问题如下。

我正在发出请求:https://localhost:44364/api/businessAttributes/contractors/foo 并且请求到达了我的控制器:我能够调试控制器并看到一切正常。我也收到了回复:[{"code":"1code","title":"\"foo\" short (1code)","id":"1code"}]

然后我尝试以下请求:https://localhost:44364/api/businessAttributes/contractors/%22foo%22 并且该请求根本没有到达我的控制器。

这是我的路线在 BE 上的样子:

    [HttpGet]
    [Route("api/businessAttributes/contractors/{searchString?}")]
    public async Task<IEnumerable<BusinessAttributeSimpleListEntryDto>> GetContractors(string searchString = null)

我在这里错过了什么?

解决我能够找到的问题的唯一方法是将控制器更改为

    [HttpGet]
    [Route("api/businessAttributes/contractors")]
    public async Task<IEnumerable<BusinessAttributeSimpleListEntryDto>> GetContractors([FromUri]string searchString = null)

以及对https://localhost:44364/api/businessAttributes/contractors?searchString=%22foo%22的请求。

有没有更好的方法来解决这个问题?

更新

在评论部分的建议之后,我尝试将/ 附加到请求的末尾,以获得以下请求:https://localhost:44364/api/businessAttributes/contractors/%22foo%22/。这没有帮助。

【问题讨论】:

  • 而且您使用 url localhost:44364/api/businessAttributes/contractors/%22foo%22 发布的图像的状态为 200,如果未找到它应该返回 404。您能否检查您是否正确处理数据或放置一个断点来检查它是否被命中
  • @ShahidManzoorBhat,the request do not reach my controller at all.。我尝试放置断点,它适用于https://localhost:44364/api/businessAttributes/contractors/foo,但不适用于https://localhost:44364/api/businessAttributes/contractors/%22foo%22
  • 我尝试创建一个类似的端点并从邮递员那里打了一个类似的电话我能够打到端点https://localhost:44364/api/businessAttributes/contractors/%22foo%22

标签: c# asp.net url routing urlencode


【解决方案1】:

您必须将 " url 编码为 %22

您可以简单地使用EscapeUriString

string encodedUrl = Uri.EscapeUriString(url);

到达端点后,您的 searchString 将是 "\"foo\"""foo" 而不是 foo。确保你处理好。

【讨论】:

  • 感谢您的宝贵时间。但是,请更仔细地阅读这个问题。您建议的解决方案已尝试。
  • 另外,这里的问题你好像不明白。我在客户端有问题,而您在服务器端做某事,或者 js 是否有 string 类型的关键字?
  • 你能贴出你调用这个的js代码吗
  • 对不起。我无权访问 js。
猜你喜欢
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 2011-10-19
相关资源
最近更新 更多