13

I have got an MVC website on Azure. I have written a controller action to stand in for a resource, that should return HTTP 404, but the body content should be some HTML where I explain the reason for the 404. This is implemented as a standard action that sets Response.StatusCode. This works fine locally, but when deployed to Azure, I do not get my custom view, but just an error message in plain text. I have removed <customErrors> on Azure for debugging, with the same result.

This is the raw response received when deployed to Azure:

HTTP/1.1 404 Not Found
Cache-Control: private
Content-Length: 103
Content-Type: text/html
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By: ARR/2.5
X-Powered-By: ASP.NET
Date: Sat, 17 Aug 2013 17:24:19 GMT

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Also important, if I remove the route serving this, I get a standard .NET 404 error page, so I guess my custom action is getting run. The action is just straight forward:

    [HttpGet]
    public ActionResult LegacyMedia(string originalUrl)
    {
        ViewBag.OriginalUrl = originalUrl;
        return new ViewResult404() {ViewName = "LegacyMedia"};
    }

    public class ViewResult404 : ViewResult
    {
        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.StatusCode = (int) HttpStatusCode.NotFound;
            base.ExecuteResult(context);
        }
    }

How can I get my view rendered while responding HTTP Status 404 on Azure ?

driis
  • 161,458
  • 45
  • 265
  • 341
  • 1
    @PKKG That is not a duplicate, this question is not about customErrors (I had customErrors enabled, but disabled them to try debugging the issue). – driis Aug 17 '13 at 17:46
  • +1 for the question. I am curious to know the way to debug 404 error. and I think, I have two suggestions. You can use request_End event in Global.axax file and second suggestion is to use Fiddler. – Imad Alazani Aug 17 '13 at 17:58
  • If I remove just the line setting the status code, I get the desired view as expected, but (obviously) with a 200 status code. I got the raw response as posted above from Fiddler – driis Aug 17 '13 at 18:00

1 Answers1

29

I was able to fix this by adding this httpErrors entry to the web.config:

<configuration>  
  <system.webServer>  
    <httpErrors existingResponse="PassThrough"/>  
  </system.webServer>  
<configuration>

I found this answer here:

http://blog.dezfowler.com/2010/09/aspnet-custom-error-not-shown-in-azure.html

nklhead
  • 336
  • 3
  • 5