如何使自定义错误页的ASP

如何使自定义错误页的ASP

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

问题描述

我要为500,404和403所示。这里自定义错误页是我做了什么:

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


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

  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>


  • 注册 HandleErrorAttribute 一个FilterConfig 类全球行动过滤器如下:

  • 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());
    }
    


  • 创建自定义错误页的每个上述消息的。默认的500已经是现成可用的。

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

    声明中的每个自定义错误页观点,即该页面的模式是 System.Web.Mvc.HandleErrorInfo

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

    有关500,它显示了自定义错误页。对于其他人,它没有。

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

    有我丢失的东西?

    它看起来像这样是不是所有有,因为我通过在 onException的方法code读取显示自定义错误> HandleErrorAttribute 类,它是只有500处理。

    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?

    推荐答案

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

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

    和意见只是你实现它们的方式。我倾向于添加一点逻辑虽然,以显示堆栈跟踪如果应用程序是在调试模式下的错误信息。所以Error.cshtml看起来是这样的:

    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工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-14 10:38