如何使自定义错误页面在 ASP.NET MVC 4 中工作 [英] How to make custom error pages work in ASP.NET MVC 4

查看:22
本文介绍了如何使自定义错误页面在 ASP.NET MVC 4 中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个显示 500、404 和 403 的自定义错误页面.这是我所做的:

  1. 在 web.config 中启用自定义错误如下:

    <错误状态代码=403"redirect="~/Views/Shared/UnauthorizedAccess.cshtml"/><错误状态代码="404"redirect="~/Views/Shared/FileNotFound.cshtml"/></customErrors>

  2. HandleErrorAttribute注册为FilterConfig类中的全局操作过滤器,如下所示:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters){过滤器.添加(新的CustomHandleErrorAttribute());过滤器.添加(新授权属性());}

  3. 为上述每条消息创建了一个自定义错误页面.500 的默认值已经开箱即用.

  4. 在每个自定义错误页面视图中声明页面模型为System.Web.Mvc.HandleErrorInfo

对于 500,它显示自定义错误页面.对于其他人,它没有.

有什么我遗漏的吗?

当我通读 HandleErrorAttribute 类的 OnException 方法中的代码时,看起来这并不是显示自定义错误的全部内容,它仅处理500.

我需要做什么来处理其他错误?

解决方案

我当前的设置(在 MVC3 上,但我认为它仍然适用)依赖于有一个 ErrorController,所以我使用:

<customErrors mode="On" defaultRedirect="~/Error"><error redirect="~/Error/NotFound" statusCode="404"/></customErrors></system.web>

控制器包含以下内容:

公共类 ErrorController : 控制器{公共视图结果索引(){返回视图(错误");}公共 ViewResult NotFound(){Response.StatusCode = 404;//您可能希望将其设置为 200返回视图(未找到");}}

视图就是你实现它们的方式.不过,我倾向于添加一些逻辑,以在应用程序处于调试模式时显示堆栈跟踪和错误信息.所以 Error.cshtml 看起来像这样:

@model System.Web.Mvc.HandleErrorInfo@{布局 = "_Layout.cshtml";ViewBag.Title = "错误";}<div class="list-header clearfix"><span>错误</span>

<div class="list-sfs-holder"><div class="alert alert-error">发生意外的错误.请联系系统管理员.

@if(模型!= null && HttpContext.Current.IsDebuggingEnabled){<div><p><b>例外:</b>@Model.Exception.Message
<b>控制器:</b>@Model.ControllerName
<b>动作:</b>@Model.ActionName</p><div style="overflow:scroll"><预>@Model.Exception.StackTrace

}

I want a custom error page shown for 500, 404 and 403. Here's what I have done:

  1. Enabled custom errors in the web.config as follows:

    <customErrors mode="On" 
                  defaultRedirect="~/Views/Shared/Error.cshtml">
    
        <error statusCode="403" 
               redirect="~/Views/Shared/UnauthorizedAccess.cshtml" />
    
        <error statusCode="404" 
               redirect="~/Views/Shared/FileNotFound.cshtml" />
    
    </customErrors>
    

  2. Registered HandleErrorAttribute as a global action filter in the FilterConfig class as follows:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new CustomHandleErrorAttribute());
        filters.Add(new AuthorizeAttribute());
    }
    

  3. Created a custom error page for each of the above messages. The default one for 500 was already available out of the box.

  4. Declared in each custom error page view that the model for the page is System.Web.Mvc.HandleErrorInfo

For 500, it shows the custom error page. For others, it doesn't.

Is there something I am missing?

It does look like this is not all there is to displaying custom errors as I read through the code in the OnException method of the HandleErrorAttribute class and it is handling only 500.

What do I have to do to handle other errors?

解决方案

My current setup (on MVC3, but I think it still applies) relies on having an ErrorController, so I use:

<system.web>
    <customErrors mode="On" defaultRedirect="~/Error">
      <error redirect="~/Error/NotFound" statusCode="404" />
    </customErrors>
</system.web>

And the controller contains the following:

public class ErrorController : Controller
{
    public ViewResult Index()
    {
        return View("Error");
    }
    public ViewResult NotFound()
    {
        Response.StatusCode = 404;  //you may want to set this to 200
        return View("NotFound");
    }
}

And the views just the way you implement them. I tend to add a bit of logic though, to show the stack trace and error information if the application is in debug mode. So Error.cshtml looks something like this:

@model System.Web.Mvc.HandleErrorInfo
@{
    Layout = "_Layout.cshtml";
    ViewBag.Title = "Error";
}
<div class="list-header clearfix">
    <span>Error</span>
</div>
<div class="list-sfs-holder">
    <div class="alert alert-error">
        An unexpected error has occurred. Please contact the system administrator.
    </div>
    @if (Model != null && HttpContext.Current.IsDebuggingEnabled)
    {
        <div>
            <p>
                <b>Exception:</b> @Model.Exception.Message<br />
                <b>Controller:</b> @Model.ControllerName<br />
                <b>Action:</b> @Model.ActionName
            </p>
            <div style="overflow:scroll">
                <pre>
                    @Model.Exception.StackTrace
                </pre>
            </div>
        </div>
    }
</div>

这篇关于如何使自定义错误页面在 ASP.NET MVC 4 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆