Reputation: 2514
I've had this issue for nearly half a year, my personal website http://WayneYe.com was deployed on Windows azure (C# 4.5, ASP.NET not MVC, IIS 8.0), I consulted many SO threads like:
ASP.NET MVC 404 Error Handling
Non of them work for my situation! My web.config is like below:
<customErrors mode="Off"></customErrors>
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<remove statusCode="502" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="~/404.aspx" responseMode="ExecuteURL" />
<error statusCode="500" prefixLanguageFilePath="" path="~/500.aspx" responseMode="ExecuteURL" />
</httpErrors>
Please don't tell me to use "
<customErrors mode="RemoteOnly" defaultRedirect="~/500.aspx">
<error statusCode="404" redirect="~/404.aspx" />
</customErrors>
"
It's not working at all! If you try to visit my website with any non-exist path, you will get a 502 response (instead of a 404 response). For example:
Request URL:http://wayneye.com/dsgdsgfsdf
Request Method:GET
Status Code:502 Bad Gateway
Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:Wayne_SessionId=xtdiph5cz04e0tbsvl4bb2lw; WAWebSiteSID=d0762bbc4bf84201bfc0c7290b6ebad2;
Host:wayneye.com
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
Response Headers
Content-Length:0
Date:Mon, 10 Feb 2014 10:00:58 GMT
Server:Microsoft-IIS/8.0
Below are picked from HTTP raw log:
2014-02-17 14:37:30 WAYNEYE GET /foo X-ARR-LOG-ID=4963d014-3330-440d-93de-a4344d5188d1 80 - 116.197.226.215 Mozilla/5.0+(Windows+NT+6.3;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/32.0.1700.107+Safari/537.36 WAWebSiteSID=8822178ae255478a8447b25a055a6422;+Wayne_SessionId=ypztkdccxub13ajwjo1yvmn4;+ARRAffinity=11a7911d5cb3cbede8a042bc6547c4a522b36efb32d6fc5b60eacccf8b8db7f0;+LoginName=Wayne;+__atuvc=0%7C4%2C0%7C5%2C5%7C6%2C0%7C7%2C3%7C8 - wayneye.com 502 3 13 288 964 15
Appreciate any helps!
Upvotes: 2
Views: 2951
Reputation: 2514
Eventually figured out, in "<httpErrors>
" section, "path" cannot contain "~"! After removing "~" 404 redirection works for me both development/production environment.
The ideal situation we want:
To achieve this, the best practice here is in web.Release.config "enable" the 404/500 redirection, please be aware of "xdt:Transform="Insert"":
<httpErrors errorMode="Custom" existingResponse="Insert" defaultResponseMode="ExecuteURL" xdt:Transform="Insert">
<clear />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" path="/404.aspx" responseMode="ExecuteURL" />
<error statusCode="500" path="/500.aspx" responseMode="ExecuteURL" />
</httpErrors>
So in development environment (i.e. web.debug.config), we can still get the default detailed exception page instead of redirecting to 500:
<httpErrors errorMode="Detailed" existingResponse="Auto" />
Upvotes: 2