HandleErrorInfo using MVC2 - Model is null?

前端 未结 1 1053
南方客
南方客 2021-01-03 04:07

I have an MVC 2 web application, which is nearing release. Until now I have had custom errors turned off but I would like them working when I go production ready.

I

相关标签:
1条回答
  • 2021-01-03 04:12

    The HandleErrorInfo is null because you are performing a redirect in customErrors.

    This is the idea I'm trying out in my latest project and I updated for MVC 2. I didn't use customErrors because i can't invoke a controller action without performing a redirect (i guess).

    Application Error

    protected void Application_Error(Object sender, EventArgs e)
    {
        GlobalErrorHandler.HandleError(((HttpApplication)sender).Context, Server.GetLastError(), new ErrorController());
    }
    

    Global error handler

    public class GlobalErrorHandler
    {
        public static void HandleError(HttpContext context, Exception ex, Controller controller)
        {
            LogException(ex);
    
            context.Response.StatusCode = GetStatusCode(ex);
            context.ClearError();
            context.Response.Clear();
            context.Response.TrySkipIisCustomErrors = true;
    
            if (IsAjaxRequest(context.Request))
            {
                ReturnErrorJson(context, ex);
                return;
            }
    
            ReturnErrorView(context, ex, controller);
        }
    
        public static void LogException(Exception ex)
        {
            // log the exception
        }
    
        private static void ReturnErrorView(HttpContext context, Exception ex, Controller controller)
        {
            var routeData = new RouteData();
            routeData.Values["controller"] = "Error";
            routeData.Values["action"] = GetActionName(GetStatusCode(ex));
    
            controller.ViewData.Model = new HandleErrorInfo(ex, " ", " ");
            ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(context), routeData));
        }
    
        private static void ReturnErrorJson(HttpContext context, Exception ex)
        {
            var json = string.Format(@"success: false, error:""{0}""", ex.Message);
            context.Response.ContentType = "application/json";
            context.Response.Write("{" + json + "}");
        }
    
        private static int GetStatusCode(Exception ex)
        {
            return ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
        }
    
        private static bool IsAjaxRequest(HttpRequest request)
        {
            return request.Headers["X-Requested-With"] != null && request.Headers["X-Requested-With"] == "XMLHttpRequest";
        }
    
        private static string GetActionName(int statusCode)
        {
            string actionName;
    
            switch (statusCode)
            {
                case 404:
                    actionName = "NotFound";
                    break;
    
                case 400:
                    actionName = "InvalidRequest";
                    break;
    
                case 401:
                    actionName = "AccessDenied";
                    break;
    
                default:
                    actionName = "ServerError";
                    break;
            }
    
            return actionName;
        }
    
        public static bool IsDebug
        {
            get
            {
                bool debug = false;
    
    #if DEBUG
                debug = true;
    #endif
                return debug;
            }
        }
    }
    

    Error controller

    public class ErrorController : Controller
    {
        public ActionResult AccessDenied()
        {
            return View("AccessDenied");
        }
    
        public ActionResult InvalidRequest()
        {
            return View("InvalidRequest");
        }
    
        public ActionResult NotFound()
        {
            return View("NotFound");
        }
    
        public ActionResult ServerError()
        {
            return View("ServerError");
        }
    }
    

    ServerError view

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        ServerError
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
        <h2>ServerError</h2>
    
        <% if (Model.Exception != null ) { %>
            <p>
              Controller: <%= Model.ControllerName %>
            </p>
            <p>
              Action: <%= Model.ActionName %>
            </p>
            <p>
              Message: <%= Model.Exception.Message%>
            </p>
            <p>
              Stack Trace: <%= Model.Exception.StackTrace%>
            </p>
        <% } %>
    
    </asp:Content>
    
    0 讨论(0)
提交回复
热议问题