Asp.net mvc覆盖基本控制器中的OnException,继续传播到Application_Error

Har*_*oon 50 c# error-handling exception-handling asp.net-mvc-2

我试图返回一个视图,不会根据我的应用程序可能发生的某些错误向用户发出重定向,我想处理错误+在基本控制器中记录它们,我不希望错误传播到我的Global.asax - Application_Error()方法因为我希望这个方法能够处理我的应用程序中的任何其他错误,例如用户输入虚假URL,有没有人找到解决方法?

注意:我已经离开了我的注释代码,因为我有一些问题的解决方法,这也表明我有可能处理的多个例外...

编辑:如果我在这个OnException覆盖中发出RedirectToAction覆盖一切按预期工作,但我只想返回视图,没有重定向...

我的基本控制器方法是:

    protected override void OnException(ExceptionContext filterContext)
    {
        //dont interfere if the exception is already handled
        if (filterContext.ExceptionHandled)
            return;

        //let the next request know what went wrong
        filterContext.Controller.TempData["exception"] = filterContext.Exception;

        //log exception
        _logging.Error(User.Identity.Name, ExceptionHelper.BuildWebExceptionMessage(filterContext.Exception));


        //set up redirect to my global error handler
        //if (filterContext.Exception.GetType() == typeof(NoAccessException))
        //    filterContext.Result = View(new RouteValueDictionary
        //    (new { area = "", controller = "Error", action = "PublicError" }));

        //else {
        //Only return view, no need for redirection
        filterContext.Result = View(new RouteValueDictionary
        (new { area = "", controller = "Error", action = "NoAccess" }));
        //}
        //advise subsequent exception filters not to interfere and stop
        // asp.net from showing yellow screen of death
        filterContext.ExceptionHandled = true;

        //erase any output already generated
        filterContext.HttpContext.Response.Clear();

        //base.OnException(filterContext);
    }
Run Code Online (Sandbox Code Playgroud)

此方法应该处理可能出现在我的应用程序中的任何其他错误,我不希望上面的错误在我的Application_Error()中处理

protected void Application_Error()
        {

            Exception exception = Server.GetLastError();
            // Log the exception.

            var logger = Container.Get<ILoggingService>();
            logger.Error(User.Identity.Name, ExceptionHelper.BuildWebExceptionMessage(exception));

            Response.Clear();

            HttpException httpException = exception as HttpException;

            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "Error");

            //if (httpException == null)
            //{
            routeData.Values.Add("action", "PublicError");
            //}
            //else //It's an Http Exception, Let's handle it.
            //{
            //    switch (httpException.GetHttpCode())
            //    {
            //        case 404:
            //            // Page not found.
            //            routeData.Values.Add("action", "HttpError404");
            //            break;
            //        case 500:
            //            // Server error.
            //            routeData.Values.Add("action", "HttpError500");
            //            break;

            //        // Here you can handle Views to other error codes.
            //        // I choose a General error template  
            //        default:
            //            routeData.Values.Add("action", "General");
            //            break;
            //    }
            //}

            // Pass exception details to the target error View.
            routeData.Values.Add("error", exception);

            // Clear the error on server.
            Server.ClearError();

            // Avoid IIS7 getting in the middle
            Response.TrySkipIisCustomErrors = true;

            // Call target Controller and pass the routeData.
            IController errorController = new ErrorController();
            errorController.Execute(new RequestContext(
                 new HttpContextWrapper(Context), routeData));
        }
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 72

以下应该有效:

protected override void OnException(ExceptionContext filterContext)
{
    if (filterContext.ExceptionHandled)
    {
        return;
    }
    filterContext.Result = new ViewResult
    {
        ViewName = "~/Views/Shared/Error.aspx"
    };
    filterContext.ExceptionHandled = true;
}
Run Code Online (Sandbox Code Playgroud)

还要确保在此方法中不会抛出任何异常,否则它将传播到Application_Error.

  • 可能我的问题很愚蠢......但是谁能告诉我这个“OnException()”(上面的代码)要放在应用程序中的哪里。在任何“controller”类或“global.ascx.cs”中。在哪里? (2认同)
  • 我发现设置了一些其他属性,例如视图数据(类型为`HandleErrorInfo`的模型),响应代码也很有用http://devproconnections.com/aspnet-mvc/aspnet-mvc-tutorial-handling-errors-and-例外 (2认同)