简体   繁体   English

Asp.net mvc 覆盖基础 controller 中的 OnException 不断传播到 Application_Error

[英]Asp.net mvc override OnException in base controller keeps propagating to Application_Error

I am trying to return a view not issue a redirect to the user based on certain errors that could occur from my application, I want to handle the errors + log them inside my base controller, I do not want the error to propagate up to my Global.asax - Application_Error() method as I want this method to handle any other errors inside my app eg user enters a bogus URL, has anyone found a way around this?我正在尝试返回一个视图,而不是根据我的应用程序可能发生的某些错误向用户发出重定向,我想处理这些错误并将它们记录在我的基础 controller 中,我不希望错误传播到我的Global.asax - Application_Error() 方法,因为我希望此方法能够处理我的应用程序中的任何其他错误,例如用户输入虚假的 URL,有人找到解决方法吗?

NOTE: I have left my commented code as I had a workaround for some issues, this also shows I have multiple exceptions to possible handle...注意:我留下了我的注释代码,因为我有一些问题的解决方法,这也表明我有多个可能处理的异常......

EDIT: If I issue a RedirectToAction within this OnException override everything works as expected, but I only want to return the view and no redirection...编辑:如果我在此 OnException 中发出 RedirectToAction 覆盖,一切都按预期工作,但我只想返回视图而不是重定向......

My base controller method is:我的基础 controller 方法是:

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

This method should handle any other errors that could appear in my app, I do not want the errors above being handled inside my Application_Error()此方法应处理可能出现在我的应用程序中的任何其他错误,我不希望在我的 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));
        }

The following should work:以下应该有效:

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

Also make sure that no exception is thrown in this method or it will propagate to Application_Error .还要确保在这个方法中没有抛出异常,否则它将传播到Application_Error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从ASP.NET MVC 2中的Application_Error重定向? - How to redirect from Application_Error in ASP.NET MVC 2? 即使Application_Error完成,asp.net应用程序仍保持运行 - asp.net application keeps running even after Application_Error finishes ASP.NET MVC Controller.OnException未被调用 - ASP.NET MVC Controller.OnException not being called ASP.NET MVC如何在不重写URL的情况下处理Application_Error的404错误 - ASP.NET MVC how to handle 404 error from Application_Error without rewriting the URL ASP.NET MVC自定义错误处理Application_Error Global.asax? - ASP.NET MVC Custom Error Handling Application_Error Global.asax? 您是否应该在ASP.NET MVC的global.asax中使用方法Application_Error? - Should you use the method Application_Error in your global.asax in ASP.NET MVC? 在ASP.NET MVC4中,application_error哪里去了? - In ASP.NET MVC4, where has application_error gone? asp.net中的Application_Error与Context_Error - Application_Error vs Context_Error in asp.net 如何在Application_Error()中知道asp.net中的请求是ajax - How to know if the request is ajax in asp.net in Application_Error() ASP.NET MVC 2中的基本控制器实现 - Base controller implementation in ASP.NET MVC 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM