开发者

How can i set my own default error page in asp.net?

开发者 https://www.devze.com 2023-01-13 22:48 出处:网络
Hi if a request from my website is containing some errors(database linking errors) then asp.net host server 开发者_C百科error page is displaying but i want to avoid the error page and i hav to display

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


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


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消