Plus (+) in MVC Argument causes 404 on IIS 7.0

前端 未结 4 693
面向向阳花
面向向阳花 2020-12-14 06:54

I have an MVC route that is giving me hell on a staging server running IIS. I am running Visual Studio 2010\'s development server locally.

Here is a sample URL that

相关标签:
4条回答
  • 2020-12-14 07:19

    + only has the special meaning of being a space in application/x-www-form-urlencoded data such as the query string part of a URL.

    In other parts of the URL like path components, + literally means a plus sign. So resolving Full+Size to the unencoded name Full Size should not work anywhere.

    The only correct form of a space in a path component is %20. (It still works when you type an actual space because the browser spots the error and corrects it for you.) %20 also works in form-URL-encoded data as well, so it's generally safest to always use that.

    Sadly HttpUtility.UrlEncode is misleadingly-named. It produces + in its output instead of %20, so it's really a form-URL-encoder and not a standard URL-encoder. Unfortunately I don't know of an ASP.NET function to “really URL-encode” strings for use in a path, so all I can recommend is doing a string replace of + to %20 after encoding.

    Alternatively, avoid using spaces in path parts, eg. by replacing them with -. It's common to ‘slug’ titles being inserted to URLs, reducing them to simple alphanumerics and ‘safe’ punctuation, to avoid filling the URL with ugly %nn sequences.

    0 讨论(0)
  • 2020-12-14 07:20

    This is an IIS security setting. There is a standard request filter that rejects URLs containing + (plus) characters.

    You can disable it for your web, adding this to your web.config:

    <configuration>
       ...
       <system.webServer>
          ...
          <security>
              <requestFiltering allowDoubleEscaping="true" />
          </security>
        </system.webServer>
        ...
    </configuration>
    
    0 讨论(0)
  • 2020-12-14 07:25

    System.Web.HttpUtility.UrlPathEncode(string str) encodes a + to a %20

    0 讨论(0)
  • 2020-12-14 07:34

    Totally agree with @bobince, the problem is in the wrong encoding to %2b instead of %20

    Sadly HttpUtility.UrlEncode is misleadingly-named. It produces + in its output instead of %20, so it's really a form-URL-encoder and not a standard URL-encoder. Unfortunately I don't know of an ASP.NET function to “really URL-encode” strings for use in a path, so all I can recommend is doing a string replace of + to %20 after encoding.

    this is the important part, which is to replace the + sign with %20

    0 讨论(0)
提交回复
热议问题