13

我在 Azure 上有一个 MVC 网站。我已经编写了一个控制器操作来代替资源,它应该返回 HTTP 404,但主体内容应该是一些 HTML,我在其中解释了 404 的原因。这是作为设置Response.StatusCode. 这在本地工作得很好,但是当部署到 Azure 时,我没有得到我的自定义视图,而只是一个纯文本的错误消息。我已<customErrors>在 Azure 上删除以进行调试,结果相同。

这是部署到 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.

同样重要的是,如果我删除了提供此服务的路由,我会得到一个标准的 .NET 404 错误页面,所以我猜我的自定义操作正在运行。动作很简单:

    [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);
        }
    }

如何在 Azure 上响应 HTTP 状态 404 时呈现我的视图?

4

1 回答 1

29

我可以通过将此 httpErrors 条目添加到 web.config 来解决此问题:

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

我在这里找到了这个答案:

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

于 2013-09-22T00:10:44.543 回答