【问题标题】:WebAPI parameter with trailing space not propagated to endpoint带有尾随空格的 WebAPI 参数未传播到端点
【发布时间】:2017-09-30 17:46:09
【问题描述】:

带有路由和控制器

[HttpGet]
[Route("api/test/{term}")]
public IHttpActionResult Get(string term)
{
}

我希望将api/test/1%20(UrlEncoded "1 ")的请求作为"1 " 值填充到term 参数中。我在 web.config 的 system.web 部分启用了 <httpRuntime relaxedUrlToFileSystemMapping="true" />,因此请求可以通过,但是,尾随空格已被删除。

端点用于某种建议功能,因此值 "1""1 " 返回不同的结果。

所有关于我能找到的潜在解决方案的讨论都在谈论如何接受这样的请求:

WebAPI route 404's when there is a trailing space in the URL

Allowing reserved filenames in URLs

【问题讨论】:

  • 你可以添加一个斜杠来强制它通过尾随空格。 example.com/api/1%20
  • 是的,这有效@Hakunamatata,感谢您的建议。我意识到我还需要支持像 "11/" 这样的术语,然后会破坏路由,所以我最终从查询字符串中获取了 term 参数。

标签: c# asp.net-web-api


【解决方案1】:

Hakunamatata 分享的添加尾部斜杠的提示适用于 "1 " 等术语,但仍然不支持包含斜杠字符的字符串,我也需要支持。我最终从查询字符串中获取了term 参数。

[HttpGet]
[Route("api/test")]
public IHttpActionResult Get(string term)
{
}

支持请求格式api/test?term=TERMapi/test?term=1%20%2f

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 2012-11-04
    • 2010-12-17
    • 2016-02-26
    • 2023-01-09
    • 2012-06-27
    • 1970-01-01
    • 2014-04-25
    相关资源
    最近更新 更多