2

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:

IIS7 custom 404 not showing

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!

Community
  • 1
  • 1
Wayne Ye
  • 2,464
  • 3
  • 25
  • 29
  • Can you log using RDP and check IIS logs, Event logs etc for more details on error? Also you can try to restart your apps' host? – jan salawa Feb 13 '14 at 08:37
  • No, Azure does not provide RDP. – Wayne Ye Feb 15 '14 at 01:25
  • It does, maybe you have disabled it. http://msdn.microsoft.com/en-us/library/windowsazure/gg443832.aspx – jan salawa Feb 15 '14 at 08:01
  • I just looked for "502 BAD GATEWAY window azure" on google and found this link which I think may be helpful for you: http://social.msdn.microsoft.com/forums/windowsazure/de-de/46f9516b-4fa0-4c3e-a66d-0bc2a09a4ddb/running-windows-azure-web-site-502-bad-gateway-error – Shashank Chaturvedi Feb 17 '14 at 09:42
  • @ShashankChaturvedi No! 502 happens on all the browsers for me! You can try any non-exist path on my blog, you will get 502 regardless of what browser you use. – Wayne Ye Feb 17 '14 at 14:38
  • Just for future refrence, 502s are thrown by the loadbalancer (azure in this case) when it's unable to contact the internal site. this was most likely caused by the configuration error described below – aL3891 Mar 03 '14 at 12:06

2 Answers2

2

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:

  • Disable 404/500 redirection in development mode.
  • Enable 404/500 redirection in production mode.

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" />
Wayne Ye
  • 2,464
  • 3
  • 25
  • 29
0

If getting on your server 502 error handling to 404 page of try yet more articles:

  1. Custom Error Handling in web.config / Global.asax not handling non-existant directory
  2. Best way to implement a 404 in ASP.NET
  3. Complete Example for Error Handlers
  4. Proper Handling of 404 Errors Using redirectMode
Community
  • 1
  • 1
Elyor
  • 900
  • 1
  • 12
  • 26