Avinash patil
Avinash patil

Reputation: 1799

Error handing in asp.net c#

i have a big project in asp.net & c# (not MVC). in whole project i forget to handle throw in each and every try catch block. now the problem is : if any error occur then page shows that error. what i want to do is if any Error occurs and goes to throw i want the page to be redirected to the specific page [user friendly message on that page.].

how can i do this? can i write anything that redirect the error page to other url? is there any way to handle it in IIS?

Upvotes: 0

Views: 62

Answers (1)

Tasos K.
Tasos K.

Reputation: 8079

You can set some properties in the Web.config file to handle uncaught errors

<customErrors mode="RemoteOnly" redirectMode="ResponseRewrite" defaultRedirect="/errors/error">
    <error statusCode="404" redirect="/errors/error404" />
    <error statusCode="500" redirect="/errors/error500" />
</customErrors>

The above setup loads the /errors/error page, but you can set a different page for a different error.

Further more, if you want to handle all uncaught errors, e.g. for logging purposes, you can put this code in your Global.asax file.

public void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    // Do something with ex
}

Upvotes: 3

Related Questions