How can I make a custom error page in ASP.NET web-pages with WebMatrix?

不羁的心 提交于 2019-12-10 14:04:57

问题


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>


来源:https://stackoverflow.com/questions/17957954/how-can-i-make-a-custom-error-page-in-asp-net-web-pages-with-webmatrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!