【发布时间】: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