根据@Amila的帖子以及该帖子的确认和完成,我有同样的问题,我在谷歌上挖了很多东西,但没有机会找到正确的答案。问题是,当您使用时ASP.Net Web Application,无论MVC使用还是旧方法,都无法实现自定义错误Webform project。
如果您使用的是此选项ASP.Net Web Application(是否使用MVC):
在我的场景中,我只想为特定的404错误定义一个自定义错误,另一个错误定义为与404错误相同:
Senario1:您的自定义页面是一个简单HTML文件,放在以下位置root:
<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="ErrorPage.html" responseMode="File" />
</httpErrors>
</system.webServer>
</configuration>
Senario2:您的自定义页面是一个
aspx页面,位于以下位置
root:
<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="ErrorPage" responseMode="Redirect" />
</httpErrors>
</system.webServer>
</configuration>
注:我取下ASPX扩展,由于RouteConfig.cs在ASP.net application,你可以使用ErrorPage.aspx,如果你喜欢,它是可选的。
Senario3:您的自定义页面是一个
aspx页面,放在以下页面中
[ex: Page folder in The root (~/Page/ErrorPage.aspx)]:
我注意到的提示是:
您不应该使用~/root寻址;所以我只是没有
~/标记地添加:
<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="Page/ErrorPage" responseMode="Redirect" />
</httpErrors>
</system.webServer>
</configuration>