更新时间:2022-10-15 11:25:08
@bet ..我觉得genericUriParserOptions不再适用于.NET 4.5或更高版本。
也@JotaBe的建议,您可能需要正确地去code中的URL请求。在大多数情况下,%2F将被自动转换为一个斜杠/所以如果你需要逃避它,你将需要取消code摆在首位的%字符..所以您的网址:将如下像:www.domain.com/api/orders/23%252F06%252F2015/customers
注意字符'%252F'将被转换为实际的%2F
修改
确定这里是一个完整的解决方案(尝试过了,工作对我来说):
假设你有一个API端点像这样:
[路线(订单/ {}日期/用户)]
公众的Htt presponseMessage获取(串号)
{
}
在web.config中,您将需要设置requestPathInvalidCharacters清空它告诉asp.net允许所有的请求
<&的System.Web GT;
<的httpRuntime targetFramework =4.5requestPathInvalidCharacters =/>
< /system.web>
< system.webServer>
<安全>
<的requestFiltering allowDoubleEscaping =真/>
< /安全>
< /system.webServer>
在客户端发送请求,您将需要确保以躲避'%'像这样的API:
www.domain.com/api/orders/23%252F06%252F2015/customers
您再需要取消code请求
[路线(订单/ {}日期/用户)]
公众的Htt presponseMessage获取(串号)
{
日期时间actualDate = DateTime.Parse(System.Net.WebUtility.UrlDe code(日期)); //日期是23/06/2015
}
I know you can apply a wildcard in the route attribute to allow /
such as date input for example:
[Route("orders/{*orderdate}")]
The problem with wildcard is only applicable to the last paramter in URI. How do I solve the issue if want to have the following URI:
[Route("orders/{orderdate}/customers")]
Update:
I know there are few options to solve the issue by refactoring the code so please do not offer a solution something like:
[Route("orders/customers/{orderdate}")]
"dd-mm-yyyy"
)
@bet.. I think the genericUriParserOptions is no longer applicable to .net 4.5 or later..
also as suggested by @JotaBe, you might need to correctly decode the url request. In most case the %2F will be automatically translated to a slash '/' so if you need to escape it you will need to decode the '%' char in the first place.. so your URL: will look something like: www.domain.com/api/orders/23%252F06%252F2015/customers
Notice the characters '%252F' will be translated to the actual '%2F'
EDIT
Ok here is the complete solution (Tried it and working for me):
Assuming you have an API endpoint like so:
[Route("orders/{date}/customers")]
public HttpResponseMessage Get(string date)
{
}
In the web.config you will need to set the requestPathInvalidCharacters to empty which tells the asp.net to allow all request
<system.web>
<httpRuntime targetFramework="4.5" requestPathInvalidCharacters=""/>
</system.web>
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
When the client sending the request to the API you will need to make sure to escape the '%' like so:
www.domain.com/api/orders/23%252F06%252F2015/customers
You then need to decode the request
[Route("orders/{date}/customers")]
public HttpResponseMessage Get(string date)
{
DateTime actualDate = DateTime.Parse(System.Net.WebUtility.UrlDecode(date)); // date is 23/06/2015
}