7

With a URL like http://abc.com/myid/ab%2fcd (where the %2f is an escaped slash), asp.net will unescape the %2f so that from my application's perspective (and from asp.net mvc's perspective) the URL is: http://abc.com/myid/ab/cd

Since my application uses asp.net mvc, this behavior can easily cause problems with routing if I want to have a route specified something like "/myid/{id}" since asp.net's unescaping will cause that route not to match.

According to the answer to this question: URL-encoded slash in URL and according to this msdn page: http://msdn.microsoft.com/en-us/library/ee656542.aspx the solution (in .Net 4.0) is to put the following in your web.config:

<uri>
    <schemeSettings>
        <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
    </schemeSettings>
</uri>   

But I can't actually get it to work - the "%2f" is still being automatically unescaped into "/". Does anyone know why the config setting might not be working for me, or have any other suggestions?

Community
  • 1
  • 1
Dan P
  • 815
  • 1
  • 9
  • 11

1 Answers1

5

An easy solution is to use a catch-all token, e.g. {controller}/{action}/{*id}

Max Toro
  • 28,282
  • 11
  • 76
  • 114
  • Agreed. The `{*id}` in the suggested route will catch the remainder of the url's path, including path separators. It seems to me that what the OP is asking for is to have the "id" be equal to "ab/cd". So @Max's solution should work. – Lee DeLapp Aug 18 '11 at 19:55
  • 2
    This will not work if you have anything else in your path. For instance if you are building a rest API and have {entity}/{id}/{action} – mirhagk May 17 '17 at 18:59
  • Whatever logic is being done also seems to boil down two slashes into a single slash. So if you were hoping to use this to url encode another url as a route parameter then you'll need to look elsewhere. – Nick Jul 20 '17 at 15:33