IIS覆盖ASP.NET中的自定义404错误页面(IIS overriding custom 404 error page in ASP.NET)

我正在尝试创建一个404错误页面,目前我已经拥有以下所有内容/尝试了以下所有内容来尝试完成此操作。 当用户键入时:

HTTP://name/something.aspx

它就像它应该的那样工作。 但是如果用户键入:

HTTP://名称/ NotAFile

没有.aspx然后IIS7自己动手,我得到了IIS7附带的可爱错误页面。 目标是网站仅重定向404状态代码(因此不是200或302重定向)。 我在web配置中尝试过:

<customErrors mode="On" defaultRedirect="~/error/Default.aspx redirectMode="ResponseRewrite"> <error statusCode="404" redirect="~/error/NotFound.aspx" /> </customErrors>

这适用于具有.aspx文件扩展名的URL,但不适用于没有扩展名的URL。 与global.asax中的此方法相同

void Application_Error(object sender, EventArgs e) { var serverError = Server.GetLastError() as HttpException; if (serverError != null) { if (serverError.GetHttpCode() == 404) { Server.ClearError(); Server.Transfer("~/error/NotFound.aspx"); } Server.Transfer("~/error/Default.aspx"); } }

对此有相同的结果:(我最后的尝试是将此应用到Web配置:

<system.webServer> <httpErrors existingResponse="PassThrough" /> </system.webServer>

有了这个我只是得到一个没有任何东西的纯白色屏幕......任何想法或评论将不胜感激! 提前致谢!

I am trying to create a 404 error page and currently I have all of the following/tried all of the following to try and accomplish this. When the user types in :

http://name/something.aspx

It works just as its supposed to. But if the user types in:

http://name/NotAFile

with no .aspx then IIS7 takes matters into its own hands and I get the lovely error page that IIS7 comes with. The goal is that the site redirects with only a 404 status code (so not a 200, or a 302 redirect). I have tried in both the web config with:

<customErrors mode="On" defaultRedirect="~/error/Default.aspx redirectMode="ResponseRewrite"> <error statusCode="404" redirect="~/error/NotFound.aspx" /> </customErrors>

This works for the url with a .aspx file extension but not for no extension. Same with this approach in the global.asax

void Application_Error(object sender, EventArgs e) { var serverError = Server.GetLastError() as HttpException; if (serverError != null) { if (serverError.GetHttpCode() == 404) { Server.ClearError(); Server.Transfer("~/error/NotFound.aspx"); } Server.Transfer("~/error/Default.aspx"); } }

The same results are present for this :( My final attempt was to apply this to the web config:

<system.webServer> <httpErrors existingResponse="PassThrough" /> </system.webServer>

With this I just get a plain white screen with nothing on it... Any thoughts or comments would be greatly appreciated!! Thanks in advance!

最满意答案

您的应用程序似乎在经典管道模式下运行。 将其更改为集成,您的问题将得到解决。 这是一篇关于管道模式及其差异的文章 - http://learn.iis.net/page.aspx/508/wildcard-script-mapping-and-iis-7-integrated-pipeline/

It seems that your application is running in classic pipeline mode. Change it to integrated and your problem will be fixed. Here is an article about pipeline modes and their differences - http://learn.iis.net/page.aspx/508/wildcard-script-mapping-and-iis-7-integrated-pipeline/

IIS覆盖ASP.NET中的自定义404错误页面(IIS overriding custom 404 error page in ASP.NET)

我正在尝试创建一个404错误页面,目前我已经拥有以下所有内容/尝试了以下所有内容来尝试完成此操作。 当用户键入时:

HTTP://name/something.aspx

它就像它应该的那样工作。 但是如果用户键入:

HTTP://名称/ NotAFile

没有.aspx然后IIS7自己动手,我得到了IIS7附带的可爱错误页面。 目标是网站仅重定向404状态代码(因此不是200或302重定向)。 我在web配置中尝试过:

<customErrors mode="On" defaultRedirect="~/error/Default.aspx redirectMode="ResponseRewrite"> <error statusCode="404" redirect="~/error/NotFound.aspx" /> </customErrors>

这适用于具有.aspx文件扩展名的URL,但不适用于没有扩展名的URL。 与global.asax中的此方法相同

void Application_Error(object sender, EventArgs e) { var serverError = Server.GetLastError() as HttpException; if (serverError != null) { if (serverError.GetHttpCode() == 404) { Server.ClearError(); Server.Transfer("~/error/NotFound.aspx"); } Server.Transfer("~/error/Default.aspx"); } }

对此有相同的结果:(我最后的尝试是将此应用到Web配置:

<system.webServer> <httpErrors existingResponse="PassThrough" /> </system.webServer>

有了这个我只是得到一个没有任何东西的纯白色屏幕......任何想法或评论将不胜感激! 提前致谢!

I am trying to create a 404 error page and currently I have all of the following/tried all of the following to try and accomplish this. When the user types in :

http://name/something.aspx

It works just as its supposed to. But if the user types in:

http://name/NotAFile

with no .aspx then IIS7 takes matters into its own hands and I get the lovely error page that IIS7 comes with. The goal is that the site redirects with only a 404 status code (so not a 200, or a 302 redirect). I have tried in both the web config with:

<customErrors mode="On" defaultRedirect="~/error/Default.aspx redirectMode="ResponseRewrite"> <error statusCode="404" redirect="~/error/NotFound.aspx" /> </customErrors>

This works for the url with a .aspx file extension but not for no extension. Same with this approach in the global.asax

void Application_Error(object sender, EventArgs e) { var serverError = Server.GetLastError() as HttpException; if (serverError != null) { if (serverError.GetHttpCode() == 404) { Server.ClearError(); Server.Transfer("~/error/NotFound.aspx"); } Server.Transfer("~/error/Default.aspx"); } }

The same results are present for this :( My final attempt was to apply this to the web config:

<system.webServer> <httpErrors existingResponse="PassThrough" /> </system.webServer>

With this I just get a plain white screen with nothing on it... Any thoughts or comments would be greatly appreciated!! Thanks in advance!

最满意答案

您的应用程序似乎在经典管道模式下运行。 将其更改为集成,您的问题将得到解决。 这是一篇关于管道模式及其差异的文章 - http://learn.iis.net/page.aspx/508/wildcard-script-mapping-and-iis-7-integrated-pipeline/

It seems that your application is running in classic pipeline mode. Change it to integrated and your problem will be fixed. Here is an article about pipeline modes and their differences - http://learn.iis.net/page.aspx/508/wildcard-script-mapping-and-iis-7-integrated-pipeline/