ASP.NET MVC 5错误处理 [英] ASP.NET MVC 5 error handling

查看:259
本文介绍了ASP.NET MVC 5错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们要处理403错误,404错误,由于 MySpecialDomainException 所有错误,并为所有其他错误(包括IIS配置中的错误)提供默认错误页面! )。所有错误都应该返回适​​当的Razor视图,在视图前面有一个 ErrorController 将是非常好的。 例如

We want to handle 403 errors, 404 errors, all errors due to a MySpecialDomainException and provide a default error page for all other errors (including errors in the IIS configuration!). All errors should return proper Razor views, it would be very nice to have an ErrorController in front of the views. E.g. something like this:

public class ErrorController : Controller
{
    public ViewResult NotFound () { return View(); }
    public ViewResult Forbidden () { return View(); }
    public ViewResult Default ()
    {
        var ex = ObtainExceptionFromSomewhere();
        if(ex is MySpecialDomainException)
            return View("MySpecialDomainException", new ErrorModel { Exception = ex });

        return View("GeneralError", new ErrorModel { Exception = ex });
    }
}

目前,您会发现许多不同的方法www,有些可能是过时的。其中:

Currently you find many different ways to do that on the www, some most probably outdated. Among those:


  • Controller.OnException()

  • 错误过滤器

  • web.config中的customErrors元素

  • 处理Global.asax的Application_Error

  • Controller.OnException()
  • Error filter
  • customErrors element in web.config
  • Handling in Global.asax's Application_Error

Q1:使用ASP.NET MVC 5,推荐使用什么方式来满足我们的需求?

Q1: What is the recommended way to fulfill our requirements with ASP.NET MVC 5?

我们还要捕获IIS中发生的错误主办。 Q2:为了防止IIS处理任何404,我们考虑添加一个匹配所有可能的URL的默认路由 - 这是可以推荐的吗?更好的注册为IIS的404s?

Also we want to catch errors occurring in the IIS host. Q2: To prevent that IIS has to handle any 404s we thought about adding a default route matching all possible URLs - is this recommendable? Better to register instead for IIS' 404s as well?

Q3:甚至可以注册一个IIS错误页面,可以追溯到控制器,还是IIS能够ASPX /静态HTML?

Q3: Is it even possible to register an IIS error page which goes back to a controller, or is IIS capable of ASPX / static HTML only?

推荐答案

最好的方法是使用Global.Asax,因为你可以管理所有类型的错误(Ajax调用/所有意外的错误)。与其他人不能做。

the best way is using Global.Asax, because you can manage all types of errors (Ajax calls/ all of unexpected Errors). with others you can't do it.

像这样:

protected void Application_Error()
    {
        HttpContext httpContext = HttpContext.Current;
        if (httpContext != null)
        {
            RequestContext requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext;
            /* when the request is ajax the system can automatically handle a mistake with a JSON response. then overwrites the default response */
            if (requestContext.HttpContext.Request.IsAjaxRequest())
            {
                httpContext.Response.Clear();
                string controllerName = requestContext.RouteData.GetRequiredString("controller");
                IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
                IController controller = factory.CreateController(requestContext, controllerName);
                ControllerContext controllerContext = new ControllerContext(requestContext, (ControllerBase)controller);

                JsonResult jsonResult = new JsonResult();
                jsonResult.Data = new { success = false, serverError = "500" };
                jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                jsonResult.ExecuteResult(controllerContext);
                httpContext.Response.End();
            }
            else
            {
                httpContext.Response.Redirect("~/Error");
            }
        }
    }

这篇关于ASP.NET MVC 5错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆