404 handling in Azure website

人盡茶涼 提交于 2020-01-31 04:30:46

问题


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 ?


回答1:


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



来源:https://stackoverflow.com/questions/18291550/404-handling-in-azure-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!