• MVC HandleErrorAttribte特性


      MVC默认提供了一个异常过滤器 HandleErrorAttribte

    我们有2种方式可以使用它,一是在类或者方法上直接使用HandleError属性来定义:

    // 在这里声明
    [HandleError]
    public class HomeController : Controller
    {
    // 或者在这里声明
    // [HandleError]
    public ActionResult Index()
    {
    return View();
    }
    }


    另外一种方式是Global Filters功能来注册,代码如下:

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

    public static void RegisterRoutes(RouteCollection routes)
    {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

    }

    protected void Application_Start()
    {
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    }


    代码段里的filters.Add(new HandleErrorAttribute());设置是说整个程序所有的Controller都使用这个HandleErrorAttribute来处理错误。

    注意:HandleErrorAttribute只处理500系列错误,所以404错误需要另外单独处理,稍后会提到。


    下一步,我们要做的是开启web.config根目录里的customErrors(不是views目录下的那个web.config哦),代码如下:

    <customerrors mode="On" defaultredirect="~/Error/HttpError">
    <error redirect="~/Error/NotFound" statuscode="404" />
    </customerrors>

    defaultredirect是设置为所有错误页面转向的错误页面地址,而里面的error元素可以单独定义不同的错误页面转向地址,上面的error行就是定义404所对应的页面地址。

    最后一件事,就是定义我们所需要的错误页面的ErrorController:

    public class ErrorController : BaseController
    {
    //
    // GET: /Error/
    public ActionResult HttpError()
    {
    return View("Error");
    }
    public ActionResult NotFound()
    {
    return View();
    }
    public ActionResult Index()
    {
    return RedirectToAction("Index", "Home");
    }
    }


    默认Error的view是/views/shared/Error.cshtml文件,我们来改写一下这个view的代码,代码如下:

    @model System.Web.Mvc.HandleErrorInfo
    @{
    ViewBag.Title = "General Site Error";
    }

    <h2>A General Error Has Occurred</h2>

    @if (Model != null)
    {
    <p>@Model.Exception.GetType().Name<br />
    thrown in @Model.ControllerName @Model.ActionName</p>
    <p>Error Details:</p>
    <p>@Model.Exception.Message</p>
    }


    你也可以通过传递参数来重写GlobalFilter里的HandleErrorAttribte注册,单独声明一个特定的Exception,并且带有Order参数,当然也可以连续声明多个,这样就会多次处理。

    filters.Add(new HandleErrorAttribute
    {
    ExceptionType = typeof(YourExceptionHere),
    // DbError.cshtml是一个Shared目录下的view.
    View = "DbError",
    Order = 2
    });
  • 相关阅读:
    轻松搭建CAS 5.x系列(7)-在CAS Server使用第三方帐号做认证
    轻松搭建CAS 5.x系列(6)-在CAS Server上增加OAuth2.0协议
    轻松搭建CAS 5.x系列(5)-增加密码找回和密码修改功能
    CAS 5.x搭建常见问题系列(3).Failure to find org.apereo.cas:cas-server-support-pm-jdbc:jar:5.1.9
    CAS 5.x搭建常见问题系列(2).PKIX path building failed
    CAS 5.x搭建常见问题系列(1).未认证的授权服务
    轻松搭建CAS 5.x系列(4)-Java客户端程序接入CAS单点登录,Hello World版
    轻松搭建CAS 5.x系列文章
    CAS实现SSO单点登录-CAS Server 5.3搭建 cas5.3搭建 cas5.3去除https cas 去除https cas 5.x 去除https
    互联网大咖都要收藏的几个网站,纯干货
  • 原文地址:https://www.cnblogs.com/qtiger/p/10824562.html
Copyright © 2020-2023  润新知