How can I make a custom error page in ASP.NET web-

2019-06-22 11:59发布

问题:

Believe it or not I tried to look for the answer to this question with a simple Google Search but I didn't find anything (Googled with "WebMatrix custom error page", "WebMatrix how to make custom server-side error page", etc.), but perhaps I am not searching with the correct terms...

Anyway, I was just wondering if there was a way (I believe it involves the web.config file) to show a custom-made error page instead of ANY server-side error page.

I know there is a way to do this with some pages (like 404 or 500) but is it possible to make a catch all page for any server-side error? (I guess 404 wouldn't work, since it has to find your site to show any custom page?)

Please forgive me if this is a repeat question, but my lack of knowledge in doing this has possibly left me without the correct search terms to search on, although I have tried searching SE, as well.

回答1:

Add the following to your web.config file within the <system.web> node:

<customErrors mode="On" defaultRedirect="~/Error.cshtml" />

This will redirect the user to Error.cshtml (which you need to create) in the event of any ASP.NET error. You can change the mode value to RemoteOnly during development, so that you can see the actual error message.

If you want a custom 404 page as well, you can do the following:

<customErrors mode="On">
    <error statusCode="500" redirect="~/Error.cshtml" />
    <error statusCode="404" redirect="~/404.cshtml" />
</customErrors>