gpt4 book ai didi

c# - 在 ASP.NET 中编码的引号阻止请求到达路由

转载 作者:行者123 更新时间:2023-12-04 10:50:38 24 4
gpt4 key购买 nike

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

就我而言,问题如下。

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

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

这是我在 BE 上的路线:

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

enter image description here
enter image description here
enter image description here
enter image description here

我在这里缺少什么?

解决我能找到的问题的唯一方法是将 Controller 更改为
    [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 编码为 %22

您可以简单地使用 EscapeUriString

string encodedUrl = Uri.EscapeUriString(url);

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

关于c# - 在 ASP.NET 中编码的引号阻止请求到达路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59489884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com