Web.config - 自定义错误页面无效 [英] Web.config - Custom error pages not working
问题描述
我尝试过以下内容(并尝试使用已注释掉的注释),但只会收到错误:
资源你正在寻找已被删除,更改名称
,或暂时不可用。
在web.config中已发布的网站项目:
< system.webServer>
< httpErrors errorMode =CustomexistingResponse =Replace>
< remove statusCode =404/>
<! - < error statusCode =404responseMode =Filepath =\ Error\404.htm/> - >
< error statusCode =404responseMode =ExecuteURLpath =http://example.com/Error/404.htm/>
< / httpErrors>
< /system.webServer>
我通过从更改浏览器中的网址来尝试... / default.aspx
(很好)到 ... / abc.aspx
。
<这是重定向到错误页面的正确方法,还是有错误?
编辑
我发现如果我尝试 http://example.com/nonExistingPage
- 它 重定向到错误页面。但不是来自 http://example.com/Folder/nonExistingPage
EDIT 2
通过在 example.com
之后指定路径,可以部分解决问题。但是 - 该网站发布到 example.com/subfolder
,当有人导航到 example.com/nonExistingFolder
时 - 自定义错误页面未显示。
在 web.config
(还包括500个错误支持):
< configuration>
...
< system.web>
...
< customErrors mode =OnredirectMode =ResponseRewritedefaultRedirect =〜/ Error / 500.htm>
< error statusCode =404redirect =〜/ Error / 404.htm/>
< error statusCode =500redirect =〜/ Error / 500.htm/>
< / customErrors>
...
< /system.web>
...
< system.webServer>
...
< httpErrors errorMode =Custom>
< remove statusCode =404/>
< error statusCode =404path =/ Error / 404.htmresponseMode =ExecuteURLprefixLanguageFilePath =/>
< remove statusCode =500/>
< error statusCode =500path =/ Error / 500.htmresponseMode =ExecuteURLprefixLanguageFilePath =/>
< / httpErrors>
...
< /system.webServer>
...
< / configuration>
我还建议使用 .aspx
页面而不是 .htm
,以便您可以确保在响应标头中设置正确的状态代码。
<%@ Page Language =C#%>
<%Response.StatusCode = 404; %GT;
<!DOCTYPE html>
< html>
< head runat =server>
< title> 404 Not Found< / title>
< / head>
< body>
404错误
< / body>
< / html>
I have tried the following (and also tried with the commented-out uncommented instead) but only get an error:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
In the web.config of a published website project:
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<!--<error statusCode="404" responseMode="File" path="\Error\404.htm"/>-->
<error statusCode="404" responseMode="ExecuteURL" path="http://example.com/Error/404.htm"/>
</httpErrors>
</system.webServer>
I try it by changing the url in the browser from .../default.aspx
(which is fine) to .../abc.aspx
.
Is this the correct way to redirect to error pages, or is there some mistake here?
EDIT
I've found that if I try http://example.com/nonExistingPage
- it does redirect to the error page. But not fromhttp://example.com/Folder/nonExistingPage
EDIT 2
The problem was partially solved by specifying the path after example.com
. However - the site is published to example.com/subfolder
and when someone navigates to example.com/nonExistingFolder
- the custom error page is not shown.
Try this in web.config
(includes 500 error support as well):
<configuration>
...
<system.web>
...
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/500.htm">
<error statusCode="404" redirect="~/Error/404.htm" />
<error statusCode="500" redirect="~/Error/500.htm" />
</customErrors>
...
</system.web>
...
<system.webServer>
...
<httpErrors errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="/Error/404.htm" responseMode="ExecuteURL" prefixLanguageFilePath="" />
<remove statusCode="500" />
<error statusCode="500" path="/Error/500.htm" responseMode="ExecuteURL" prefixLanguageFilePath="" />
</httpErrors>
...
</system.webServer>
...
</configuration>
I would also recommend using .aspx
pages rather than .htm
so that you can ensure the proper status code is set in the response headers.
<%@ Page Language="C#" %>
<% Response.StatusCode = 404; %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>404 Not Found</title>
</head>
<body>
404 Error
</body>
</html>
这篇关于Web.config - 自定义错误页面无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!