2

I have this route:

{controller}/{id}/{action}

Because I think it makes more sense from RESTful perspective.

The problem is that id can contain slashes (/) and those are treated as route separators even when encoded as "%2F". Even when I have this Web.config section in place:

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

Because I have id in the middle I can't employ {*id} approach which captures the rest of the route including the action.

It looks like my only option is to encode / into an RFC compliant character like !, however I do not want to do it using ad-hoc custom code inside controller. I want controller to receive id intact, already decoded. And I want my Url.Action to generate properly encoded URL. Is that too much to ask from MVC, or do I need to scatter ActionFilters and custom URL helpers around?

The only way I could find is to throw in a custom IRouteConstraint to manipulate the RouteValueDictionary it receives. That sounds like a dirty hack though: a constraint manipulating its input. God knows its side effects. Do you think this is a sane enough idea, or is there a better mechanism in ASP.NET MVC allowing that?

EDIT: This workaround only works when parsing the route, not when generating one.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
  • 1
    You could always write a [custom HttpModule](http://stackoverflow.com/questions/7251285/iis-treats-double-encoded-forward-slashes-in-urls-differently-on-the-first-reque) to handle this before it gets to the MVC pipeline. – George Stocker Oct 17 '11 at 15:40

1 Answers1

1

What you are trying to do cannot be done; this has been answered several times on SO - especially of late in relation to RavenDb which uses "/" in the Id field by default (though this can be changed)

Matt Tew
  • 1,581
  • 1
  • 9
  • 15
  • 4
    Sorry - overly simplistic answer. What I should say is that there *seems* to be no elegant way to achieve this. – Matt Tew Oct 17 '11 at 11:25
  • You mean that custom route encoding cannot be done? I don't agree. If I'm willing to use my own UrlHelper's and my own ActionFilters or custom model binders, it's very possible to have custom encoding/decoding in place. I just don't want to deal with that overhead. – Sedat Kapanoglu Oct 17 '11 at 11:25