ASP.NET Web API - 如何包含 / 作为 URL 中参数值的一部分?

问题描述 投票:0回答:1

在 ASP.NET Web API 项目中,路线是

 [Route("api/itemSoldPrice/{words}/")]
。如果使用路径
http://xxx/api/itemSoldPrice/book
,则工作正常。

但是如果URL是

http://xxx/api/itemSoldPrice/1/2L
,其中
words
1/2L
,则会抛出404错误。

然后我尝试将URL更改为

http://xxx/api/itemSoldPrice/1%2F2L%20/
,而
1%2F2L%20
是原始
words
的encodeURIComponent值。但404错误仍然存在。

如何处理URL路径中的此类特殊字符?谢谢!

public class ItemSoldPriceController : BaseApiController
{

    [HttpGet]
    [Route("api/itemSoldPrice/{words}/")]
    public IHttpActionResult GetItemSoldPriceByKeyWords([FromUri] string words)
    {
        
    }
}
asp.net .net asp.net-mvc asp.net-core asp.net-web-api
1个回答
0
投票

您提供的Url不正确,解码后包含空格。

解码后的网址:

http://xxx/api/itemSoldPrice/1/2L /

考虑使用此网址:

http://xxx/api/itemSoldPrice/1%2F2L/

这里我删除了不必要的空间。用于编码/解码的源:

网站

© www.soinside.com 2019 - 2024. All rights reserved.