Ampersand & the Request URL in IIS7

ASP.NET MVC, IIS7 Add comments

With the arrival of ASP.NET MVC and the complementary IIS7 file-extension-less request pipeline it’s finally possible to turn ugly “classic ASP.NET” query string URLs into pretty and orderly REST-style URLs, functionality that PHP had for ages. Out with http://mysite.com/catalog.aspx?category=1&product=100, here comes the more pleasing http://mysite.com/widgets&gadgets/the-useful-&-beautiful-widget

However, the ampersand in the latter will generate a “400 Bad Request” response with the default settings in IIS 7 because the ampersand (&) is not acceptable in the request for security reasons. As discussed here, it takes two measures to fix this and make the URL (I did not have to take the third measure quoted in the post, which is setting ValidateRequest=”false” in the ASP.NET MVC view page):

  1. AllowRestrictedChars http://support.microsoft.com/kb/820129
  2. Interestingly enough, enable VerificationCompatibility (http://support.microsoft.com/default.aspx?scid=kb;EN-US;826437), a measure designed for pre-SP1 ASP.NET 1.1, but necessary here even with ASP.NET 3.5 to get the ampersand URLs working.

Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

11 Responses to “Ampersand & the Request URL in IIS7”

  1. Abel Haslett Says:

    You my man, are a legend! Thank you so much for this post. I had searched for ages in Google for a solution to my ampersand problem that occurred after updating the latest service packs, and now it is resolved!

  2. Lee Kelleher Says:

    Thanks Dirk! I’ve just ran into this ampersand 400-error problem for the first time.

    Luckily, I found your post and quickly fixed it!
    I did see the posts on the ASP.NET forums, but it’s nice to read a condensed, “do this, it works” blog post, thanks!

  3. ASP.NET 400 Bad Request with restricted characters - Joshua Flanagan - Says:

    [...] a %, &, *, or : in the URL. The various fixes were scattered around different forum posts, but summed up nicely at Dirk.Net. Unfortunately, the only answer seemed to be “make a registry change” or “don’t pass those [...]

  4. Andy Says:

    Thanks!

  5. * % & : Special Characters in URL and files causing HTTP 400 Bad Request | Rizo's Says:

    [...] Finally, the solution was found @ dirk.net with an article regarding 400 bad request in IIS7 (But IIS6 also has the same problem and the solution will work there too). You can read about it here. [...]

  6. aewfjoiaewf Says:

    Doesn’t seem to work for IIS7.5, sadly. Any ideas?

  7. aewfjoiaewf Says:

    Found the solution. Problem is that I’m running 64 bit Windows but 32 bit IIS, so the registry entries are in a different location.

    The only change I needed to make was at Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\ASP.NET. Added DWORD VerificationCompatibility = 1

  8. Jeff Paetkau Says:

    I still get “Bad Request” if I add UrlRoutingModule to
    <add name=”UrlRoutingModule” …

    If I remove that line it works. Any help appreciated …

  9. Patrick Rietveld Says:

    It seems to be a solution for the ampersand problem. But it’s not. This will introduce a security risk, and that’s a far more bigger problem to me. The only solution is ‘do not ampersands in file and folder names’.

  10. dirk Says:

    @Patrick: Not sure if they’re actually a security risk, but you’re absolutely right that ampersands don’t belong in an URL and should be avoided or encoded. Google doesn’t like them either. Thanks for your comment!

  11. Saurabh Maurya Says:

    Enabling VerificationCompatibility worked for me.
    Below is link for more info
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;826437

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in