2

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).

Community
  • 1
  • 1
Nick-ACNB
  • 655
  • 1
  • 7
  • 23
  • 1
    To help with your search: the YSOD is part of the ASP.NET engine, not the .NET framework. – Omar Nov 24 '10 at 04:06

1 Answers1

1

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
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Since I do handle all exception, the Application_Error is never raised. Also, the context.Server.GetLastError() is always null. – Nick-ACNB Nov 24 '10 at 15:14
  • If you handle all exceptions you should never see the YSOD. – Darin Dimitrov Nov 24 '10 at 15:41
  • I just want to intercept the YSOD exception, send it throught e-mail to the site admin and then send the non-YSOD message to the user. – Nick-ACNB Nov 24 '10 at 17:19
  • Normally in the Application_Error handler you could intercept the exception. You might also take a look at [ELMAH](http://code.google.com/p/elmah/) which will greatly simplify this task. One advantage is that you don't need to modify your existing code. You only need to insert a section in your web.config. – Darin Dimitrov Nov 24 '10 at 17:23
  • Okay, i took off my handleError on exception, so now the application_error is raised and I can catch it, now from the HttpApplication, can I have access to the HTML from the YSOD? – Nick-ACNB Nov 24 '10 at 17:51
  • No, you can't have access to the HTML of the YSOD but you could get the exception and log the message and the stack trace which is basically the contents of the YSOD. – Darin Dimitrov Nov 24 '10 at 17:53