views:

523

answers:

5

I have a MVC.NET application with one route as follows:

routes.MapRoute("member", "member/{id}/{*name}", new { controller = "member", action = "Details", id = "" }, new { id = @"\d+" });

Thus, a link could be something like this: http://domain/member/123/any_kind_of_username

This works fine in general but if the path contains illegal characters (e.g. a double qoute: http://domain/member/123/my_"user"_name) I get a "System.ArgumentException: Illegal characters in path."

After much googling the best suggestions seems to be to make sure that the url doesn't contain any such characters. Unfortunately, that is out of my control in this case.

Is there a way to work around this?

+1  A: 

You should URL-encode the URL at the client end before submitting it. Try using the UrlHelper.Encode method.

vincebowdren
Sorry, same thing here as with the other suggestions so far. I'm afraid I'm not in control of the urls at the clients end.
hbruce
+2  A: 

Turns out you could avoid this by setting allowDoubleEscaping="false" in for requestFiltering in web.Config. I.e:

<configuration>
  <system.webServer>
    <security>
      <requestFiltering allowDoubleEscaping="false" />
    </security>
  </system.webServer>
</configuration>

Perhaps not the perfect solution (any suggestions for a better one is much appreciated), but it solves the problem.

hbruce
@hbruce: It's the best solution I found so far, thanks!
PanJanek
fairly serious side-effect -- you can no longer use characters like `+` in URLs if you enable this setting. see: http://stackoverflow.com/questions/1453218/is-enabling-double-escaping-dangerous
Jeff Atwood
A: 

What about an ampersand (&) in the url? For example: mysite.com/JellyBeans/PB&J, where "PB&J" is a parameter value in a controller action method. How do you get around MVC and the ASP.NET engine from treating that as an illegal char in the base url string? Encoding that with a %26 doesn't appear to work. Odd thing, when launching VS debug the local web server (VS built in web server) handles that case fine (both as an & and a %26), but when deployed to a web server running IIS7 the url results in a "Bad Request".

Schwarty
The VS.NET builtin webserver is quite buggy and non-standards compliant. If you run into any odd behavior, you're best off ignoring it and just using IIS.
Eamon Nerbonne
A: 

Has anybody found a solution to the ampersand & in the base URL?

We for example have site like www.mydomain.com/j&b and this works fine in IIS 6.0. How do we duplicate this behaviour in IIS 7?

John
thekaido
+1  A: 

Scott Hanselman posted a good summary on allowing illegal characters in the path.

If you really want to allow characters that are restricted, remove them from the list by editing your web.config (this will probably only work in .NET 4 and on IIS7):

<system.web>
    <httpRuntime requestValidationMode="2.0" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters="&lt;,&gt;,*,%,:,&amp;,\" />
</system.web>

You may also need to do as hbruce suggests:

<system.webServer>
    <security>
        <requestFiltering allowDoubleEscaping="true"/>
    </security>
</system.webServer>

There are still some certain paths that will not work, (such as /search/% ) as you will get the 400 "Bad Request - Invalid URL" message. The only workaround I've found is to use that part as a querystring: /search?q=% with the above steps.

thekaido