Search code examples
c#asp.netiisweb-configiis-10

Special characters causes an error in URL in ASP.NET


I am entering special characters in URL that causes an error. I would like to redirect a special page but I couldn't do it.

404 redirect is working fine but 400 redirect is not working well.

Those characters causes problem in url:

1- "%" - HTTP Error 400 - Bad Request-Invalid URL
2- "&" - HTTP Error 400
3- "<" - HTTP Error 400
4- ">" - HTTP Error 400
5- ":" - HTTP Error 400

My Web.config settings:

<system.web>
    <customErrors mode="On" redirectMode="ResponseRedirect" />
</system.web>
<system.webServer>
    <httpErrors>
        <remove statusCode="400" subStatusCode="-1" />
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="400" path="400.aspx" responseMode="Redirect" />
        <error statusCode="404" path="404.aspx" responseMode="Redirect" />
    </httpErrors>
</system.webServer>

Solution

  • As far as I know, the url Bad Request-Invalid URL is thrown by the http.sys not IIS or asp.net, so changing the custom error page is usless for this kind of issue.

    Besides, it's no way to modify the custom error page for the http.sys.

    If you want to let the http.sys allow the Invalid url, I suggest you could try to create the following DWORD values under the following registry key:

    HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\HTTP\Parameters
    
    AllowRestrictedChars  1
    

    More details, you could refer to this article.