HandleErrorInfo using MVC2 - is the model null?

I have an MVC 2 web application that is nearing release. So far, I have had user errors disabled, but I would like them to work when I prepare the production.

I installed my web.config with the following:

<customErrors mode="On" defaultRedirect="/Error/">
  <error statusCode="404" redirect="/Error/NotFound "/>
</customErrors>

404 works just fine, and NotFound is an action that directly translates into a view that simply displays a pretty standard 404 page using my own Site.Master file.

For anything other than 404, I want the user to be able to view exception details. (This is an internal application, and there is no security risk).

The Errordefault action is Indexset to return the view () that I defined. I can't figure out how to pass exception information into a view?

It looked promising:

http://devstuffs.wordpress.com/2010/12/12/how-to-use-customerrors-in-asp-net-mvc-2/

But when I use View with:

<%@ Page Title="" Language="C#" 
    MasterPageFile="~/Views/Shared/Bootstrap.Master"
    Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>

The error page itself causes an error because HandleErrorInfo is NULL. Obviously, the error in the user error causes MVC2 a number of problems, and the result is a yellow screen of death.

I assume that either I missed something on the blog or does not explain how to make HandleErrorInfo to be anything other than zero without setting the Error attribute for each of my controllers / actions.

, , MVC3 , , Razor . , . , , " MVC3".

+5
1

HandleErrorInfo null, customErrors.

, , MVC 2. customErrors, ( ).

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

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

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

<%@ 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>
+12

All Articles