在 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)
{
}
}
您提供的Url不正确,解码后包含空格。
解码后的网址:
http://xxx/api/itemSoldPrice/1/2L /
考虑使用此网址:
http://xxx/api/itemSoldPrice/1%2F2L/
这里我删除了不必要的空间。用于编码/解码的源: