Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net custom error page

Tags:

asp.net

im trying to implement a custom error page, what i want to be able to do is have a single generic error page which can display the error which occurred or other information (custom error message). when a error occurs on the website, the user should be directed to this page which shows the error message.

so for example if i had a page which was trying to update something to a database, but something went wrong, i should be redirected to the error page which will have some custom text like something like " there has been an error with bla bla bla ... please contact administrator".

hope this makes sense

thanks

like image 702
c11ada Avatar asked Aug 24 '26 20:08

c11ada


2 Answers

In your webconfig you could specify the customErrors element:

<system.web>
   <customErrors mode="RemoteOnly" defaultRedirect="Error.aspx" />
</system.web>

The defaultRedirect should be the path to your Error page. This is good to specify when general errors occur, but it won't let you access the stack trace. Otherwise you want to handle that in the gloabal.asax in the Application_Error and then redirect to the error page with the custom message.

like image 81
amurra Avatar answered Aug 26 '26 22:08

amurra


You can use the Application_Error event handler in your global.asax.cs (or global.asax.vb) file to define what happens when the user encounters an error.

I've used this before. You can get the exception, send the admin an email, and display a message to the user, or log the error to the database.

like image 40
Kibbee Avatar answered Aug 27 '26 00:08

Kibbee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!