Asp.net mvc 3 get exception

I have user errors included in webconfig and redirect to "/ Error / Trouble". It works the way it was designed. Elmah error. An error image is also displayed.

The problem is that I want to check for an error in the Trouble action of my error controller. When an error occurs, how do you access it after MVC redirects you to a special error handler?

I throw an exception if CurrentUser is null:

        if (CurrentUser == null)
        {
            var message = String.Format("{0} is not known.  Please contact your administrator.", context.HttpContext.User.Identity.Name);
            throw new Exception(message, new Exception("Inner Exception"));
        }

I want to have access to this in my custom error handler (Error / Failure). How do you access the exception?

Here is my action:

    public ActionResult Trouble()
    {
        return View("Error");
    }

Here is my view:

@model System.Web.Mvc.HandleErrorInfo

<h2>
    Sorry, an error occurred while processing your request.
</h2>
@if (Model != null)
{
    <p>@Model.Exception.Message</p>
    <p>@Model.Exception.GetType().Name<br />
    thrown in @Model.ControllerName @Model.ActionName</p>
    <p>Error Details:</p>
    <p>@Model.Exception.Message</p>
}

System.Web.Mvc.HandleErrorInfo is a model for my review of problems, and it is empty. Thank you for your help.

+5
1

:

Global.asax :

        protected void Application_Error()
    {
        var exception = Server.GetLastError();

        HttpContext.Current.Application.Lock();
        HttpContext.Current.Application["TheException"] = exception;
        HttpContext.Current.Application.UnLock();
    }

Error/Trouble :

        var caughtException = (Exception)HttpContext.Application["TheException"];
        var message = (caughtException!= null) ? caughtException.Message : "Ooops, something unexpected happened.  Please contact your system administrator";
        var ex = new Exception(message);
        var errorInfo = new HandleErrorInfo(ex, "Application", "Trouble");
        return View("Error", errorInfo);

. . - ? .

+2

All Articles