Route参数与斜线" /"在网址 [英] Route parameter with slash "/" in URL

查看:456
本文介绍了Route参数与斜线" /"在网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以在路由属性应用通配符让 / 例如日期输入:

  [路线(订单/ {*}订购日期)]

带通配符的问题是只适用于URI的最后一个参数中。我该如何解决这个问题,如果想有以下URI:

  [路线(订单/ {}订购日期/用户)]

更新:

我知道有几个选项通过重构code,以解决这一问题,所以请不要提供一个解决方案是这样的:


  1. 的路径模板更改为 [路线(订单/客户/ {}订购日期)]

  2. 更改日期为不同的格式(例如DD-MM-YYYY


解决方案

@bet ..我觉得genericUriParserOptions不再适用于.NET 4.5或更高版本。

也@JotaBe的建议,您可能需要正确地去code中的URL请求。在大多数情况下,%2F将被自动转换为一个斜杠/所以如果你需要逃避它,你将需要取消code摆在首位的%字符..所以您的网址:将如下像:www.domain.com/api/orders/23%252F06%252F2015/customers

注意字符'%252F'将被转换为实际的%2F

修改

确定这里是一个完整的解决方案(尝试过了,工作对我来说):


  1. 假设你有一个API端点像这样:

      [路线(订单/ {}日期/用户)]
    公众的Htt presponseMessage获取(串号)
    {
    }


  2. 在web.config中,您将需要设置requestPathInvalidCharacters清空它告诉asp.net允许所有的请求

     <&的System.Web GT;
        <的httpRuntime targetFramework =4.5requestPathInvalidCharacters =/>
    < /system.web>
    < system.webServer>
        <安全>
          <的requestFiltering allowDoubleEscaping =真/>
        < /安全>
    < /system.webServer>


  3. 在客户端发送请求,您将需要确保以躲避'%'像这样的API:

    www.domain.com/api/orders/23%252F06%252F2015/customers


  4. 您再需要取消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:

  1. change the route template to [Route("orders/customers/{orderdate}")]
  2. change the date to a different format (e.g. "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):

  1. Assuming you have an API endpoint like so:

    [Route("orders/{date}/customers")]
    public HttpResponseMessage Get(string date)
    {
    }
    

  2. 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>
    

  3. 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

  4. 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
    }
    

这篇关于Route参数与斜线&QUOT; /&QUOT;在网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆