asp.net-mvcexceptionyellow-screen-of-death

Can the ASP.NET MVC Yellow Screen of Death (YSOD) be generated on demand


As asked here.

I want to know if it is possible to get the YSOD's HTML Rendering for exceptions to be sent by mail WITHOUT the use of ELMAH? I am handling the errors and showing a custom error page to the user. I am also sending the exception general information throught mail, however I really would like to know if I can wrap them into the real built-in YSOD engine of ASP.NET and keep the HTML formatting.

UPDATE1:

I have my custom exceptions (DupplicatedArbsException) that returns a view with the message which i consider "Managed Exceptions". However, if it is a real error that I did not catch, it will return the Error view.

    [HandleError(ExceptionType = typeof(Exception), View = "Error")]
    [HandleError(ExceptionType = typeof(DuplicatedArbsException), View = "ErrorViewArbs")]
    public ActionResult Create(string id, int? version)
    {
         //...
    }

The HandleError Raises which does nothing currently.

    protected override void OnException(ExceptionContext filterContext)
    {
        var ex = filterContext.Exception;
        base.OnException(filterContext);
    }

..

    <customErrors mode="On" defaultRedirect="Error"/>

The exception raised in customErrors mode="off" is the YSOD from asp.net. However, when I turn customErrors mode="on" those exceptions are not wrapped in it's html equivalent but only the exception messages (no html at all).


Solution

  • You could handle the Application_Error event in global.asax which is triggered by the ASP.NET engine every-time an exception is not handled:

    protected void Application_Error(object sender, EventArgs e)
    {
        var app = (HttpApplication)sender;
        var context = app.Context;
        // get the exception that was unhandled
        Exception ex = context.Server.GetLastError();
    
        // TODO: log, send the exception by mail
    }