【ASP.NET】添加错误页,404页面

在Web.config中的system.web中添加

<!--RemoteOnly-发布时;On-测试时,会显示错误页面-->
<customErrors mode="RemoteOnly" redirectMode="ResponseRewrite" defaultRedirect="~/ErrorPage/500.aspx">
  <error statusCode="404" redirect="~/ErrorPage/404.aspx"/>
  <error statusCode="500" redirect="~/ErrorPage/500.aspx"/>
</customErrors>

在system.webServer中添加

<httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File">
  <remove statusCode="404"/>
  <remove statusCode="500"/>
  <!--api会返回整个网页源码-->
  <!--<error statusCode="404" path="/ErrorPage/404.html" responseMode="Redirect"/>
  <error statusCode="500" path="/ErrorPage/500.html" responseMode="Redirect"/>-->
  <!--api中的提示信息能正常提示-->
  <error statusCode="404" path="~/ErrorPage/404.html" responseMode="File" />
  <error statusCode="500" path="~/ErrorPage/500.html" responseMode="File" />
</httpErrors>    

项目中添加ErrorPage文件夹

404.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>404 Page Not Found</title>
</head>
<body>
    <h1>404 Page Not Found</h1>
</body>
</html>

500.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>500 Server Error</title>
</head>
<body>
    <h1>500 Server Error</h1>
</body>
</html>

404.aspx,500.aspx与上面的html文件一样可以在文件头添加

Response.StatusCode = 404

Response.StatusCode = 500

 

参考

http://dditblog.com/itshare_728.html

https://www.cnblogs.com/TiestoRay/p/4723996.html

 

 

相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页