【问题标题】:404 handling in Azure websiteAzure 网站中的 404 处理
【发布时间】:2013-08-17 17:34:04
【问题描述】:

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

这是部署到 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 时呈现我的视图?

【问题讨论】:

  • @PKKG 这不是重复的,这个问题与 customErrors 无关(我启用了 customErrors,但禁用了它们以尝试调试问题)。
  • +1 表示问题。我很想知道调试 404 错误的方法。我想,我有两个建议。您可以在 Global.axax 文件中使用 request_End 事件,第二个建议是使用 Fiddler。
  • 如果我只删除设置状态代码的行,我会按预期获得所需的视图,但(显然)带有 200 状态代码。我得到了 Fiddler 上面发布的原始回复

标签: asp.net-mvc asp.net-mvc-3 azure


【解决方案1】:

我可以通过将此 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

【讨论】:

    猜你喜欢
    • 2012-04-03
    • 2017-03-05
    • 2013-09-28
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多