Richard77
Richard77

Reputation: 21621

How to catch error in an ASP.NET MVC and display a gracious error message to the user?

I'm writing a very small application for my organization. As nobody seems to know what he wants, I've decide that I'll deploy a demo on our local server so people can get ideas on how to conceive the big thing.

Now here's the problem: While developing on my laptop, when the application crashes, I get a yellow screen giving feedback on the problem (to me). But, if I decide to put the application online, I don't want those kind of yellow screen to be seen by others who are not developers (I know they might be a few because the demo is just a starting point).

Is there anyway I can put a mechanism in place so that when there's an fatal error, the user can get a nice screen telling with an e-mail link telling him to send me an e-mail?

Thanks for helping

Upvotes: 0

Views: 2111

Answers (5)

CrazyDart
CrazyDart

Reputation: 3801

I would say the quick fix is to use try catch blocks. When you catch an error, send the user to a custom error controller and action. On this action you could have a form for them to email you... and in hidden fields you could have the exception, because you passed it to the action.

    try
    {
        int myvar = 0/4;
    }
    catch(Exception exceptionObject)
    {
        return RedirectToAction("HandledExceptionAction", "ErrorController", exceptionObject);
    }

Upvotes: 1

Nick Masao
Nick Masao

Reputation: 1108

Have a look at this I'm sure it'll solve all your problems. http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx

Upvotes: 0

samy
samy

Reputation: 14962

I'd recommend wiring ELMAH in your project, and adding the custom errors tag the other answers pointed to. This way you have a user-readable error page and full error tracing at your fingertips. You can also configure elmah to automatically send you an email with the details. The user doesn't even need to know it happened...

Priceless!

Upvotes: 3

Andy Rose
Andy Rose

Reputation: 16984

Use the customErrors section of then root web.config to specify how your application handles errors.
In your case I would think that you would want to set this to RemoteOnly so that you can still see the errors on your local machine and set 500 errors to go to your contact form:

<configuration>
  <system.web>
    <customErrors defaultRedirect="Error.htm" mode="RemoteOnly">
      <error statusCode="500" redirect="ContactAdmin.htm"/>
    </customErrors>
  </system.web>
</configuration>

Upvotes: 2

veggerby
veggerby

Reputation: 9020

Similar to classic ASP.NET by using customErrors

Upvotes: 1

Related Questions