我在 Controller 中有一个路由,它应该匹配 url 部分中的所有内容并将其放入字符串参数中。
我有的是:
[Route("api/proxy/{proxyId}/{*parameter}")]
public Task<HttpResponseMessage> Mediate(int proxyId, string parameter)
对于一个未知的 url,例如:
我希望“参数”变量包含:
test?a=1&b=2
相反,它包含:
test
我怎样才能指定路由不切断问号后的所有内容? 对于这种特殊情况,我可以从 Request.RequestUri 对象中提取它,但它会.. 不够优雅。
请您参考如下方法:
你不能那样做。根据定义,URL 段不包含查询字符串。
但是,您可以做一些非常简单的事情:在您的 WebApi Controller 中,您有包含查询字符串的 Request 属性:
Request.RequestUri.Query
您只需将 url 参数与此连接起来即可获得所需的内容。这包括前导问号:
The Query property contains any query information included in the URI. Query information is separated from the path information by a question mark (?) and continues to the end of the URI. The query information returned includes the leading question mark.
如果您仍然想强制它以不同的方式工作,您需要包含您自己的自定义路由提供程序,实现您自己的 IDirectRouteProvider并注册它。看到这个:get a list of attribute route templates asp.net webapi 2.2
但是做这样的事情是不自然的。为什么做事的方式与所有其他人理解和使用的标准方式完全不同?
