简体   繁体   中英

how could we redirect to any default page if the requested page is not found or any error occurs in asp.net

如果找不到请求的页面或在asp.net中发生任何错误,例如在错误404的情况下,我们如何重定向到任何默认页面...

You can use the customErrors tag within the web.config file.

So, you could have something like this:

<configuration>
     ...
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="~/ErrorPages/Oops.aspx">
             <error statusCode="404" redirect="~/ErrorPages/404.aspx" />
        </customErrors>
        ....
    </system.web>
</configuration>

Where Oops.aspx will be the page shown for all HTTP error codes. The inner error tag allows you to specify specific pages for certain error codes. If you don't want that behaviour, just remove the inner tag.

For more info, check out http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs

The customErrors tag in the web.config file is your friend...

<configuration>
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="~/ErrorPages/Error.aspx">
            <error statusCode="404" redirect="~/ErrorPages/404.aspx" />
        </customErrors>
    </system.web>
</configuration>

You can set up a generic catch-all page for generic errors, and then specify pages for individual status codes, allowing you to provide a custom message for 404 errors.

Also note the mode tag - "RemoteOnly" will only display custom errors for remote users, "On" will display custom errors for local users as well. Use "On" to set-up your error pages, then switch to "RemoteOnly" so you can still see exceptions while developing

http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM