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

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

问题描述

我们要处理403错误,404错误,所有的错误由于 MySpecialDomainException 并为所有其他错误(包括在IIS配置错误提供了一个默认的错误页面! )。所有的错误应该返回正确的剃刀的意见,那将是非常不错的意见前面有一个 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

Q1:什么是履行与ASP.NET MVC 5

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

此外,我们要赶在IIS主机发生的错误。 Q2:以prevent IIS具有处理我们认为有关添加默认路由匹配所有可能的URL的404 - 这是值得推荐的?更好,而不是注册为IIS404呢?

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 /静态HTMLS只有

Q3: Is it even possible to register an IIS error page which goes back to a controller, or is IIS capable of ASPX / static HTMLs 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天全站免登陆