views:

29

answers:

4

Hi if a request from my website is containing some errors(database linking errors) then asp.net host server error page is displaying but i want to avoid the error page and i hav to display my own custom designed error page instead.

+1  A: 

Set your own default error page by pasting this in your web.config file

customErrors Element (ASP.NET Settings Schema)

<configuration>
   <system.web>
      <customErrors mode="On" defaultRedirect="GetLastError.aspx" />
   </system.web>
</configuration>
Pranay Rana
@Pranay Rana The `mode` attribute set to `Off` will display the exception details. The OP doesn't want to display the exception details, but rather a custom error page, as stated in the second part of the question; with your configuration this isn't possible.
Giu
+2  A: 

Add the customErrors element as shown below to your web.config file in the appropriate place:

<configuration>
    ...

    <system.web>
        <customErrors mode="On" defaultRedirect="~/ErrorPages/Oops.aspx" />

        ...
    </system.web>
</configuration>

Setting the mode attribute to On ensures that the error page is shown to everybody. You can set the attribute to RemoteOnly if you want to show the error page to remote users only; local users will then see the error page containing the exception details instead, which may simplify development.

If you want to read more about this subject, I'd recommend to have a look at the following article from the official ASP.NET site: Displaying a Custom Error Page

Giu
It is better to post an answer and a link then just a link. Why send everyone to another site if they can get the answer here?
Oded
@Oded you're right; the link alone doesn't add much to the discussion. I therefore have updated my answer to include the details on how to achieve what the OP has asked for, and I've also left the link to the article for further reading.
Giu
+1  A: 

This MSDN Article may help you also with other approaches

Anil
+1  A: 

To achieve this, you can either use the customErrors configuration settings as Pranay mentioned. (But you'll want the mode to be either On or RemoteOnly. with RemoteOnly from the localhost you will still see the detailed error message, while other clients will see your handler page)

Also, as a sidenote, you can override the OnError method of the Page, which is called when an unhandled exception happens. To check the exception that triggered the error in that method you can call the Context.Server.GetLastError() method and proceed however you want. (log the exception, redirect to a page, etc..) A more generic solution would be to create an HttpModule to handle the Error event of the HttpApplication, but that's not what you were asking.

snomag