简体   繁体   English

IIS覆盖ASP.NET中的自定义404错误页面

[英]IIS overriding custom 404 error page in ASP.NET

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. 我正在尝试创建一个404错误页面,目前我已经拥有以下所有内容/尝试了以下所有内容来尝试完成此操作。 When the user types in : 当用户输入时:

http://name/something.aspx HTTP://name/something.aspx

It works just as its supposed to. 它的工作方式与其应有的一样。 But if the user types in: 但是如果用户键入:

http://name/NotAFile HTTP://名称/ NotAFile

with no .aspx then IIS7 takes matters into its own hands and I get the lovely error page that IIS7 comes with. 没有.aspx然后IIS7自己掌握了问题,我得到了IIS7附带的可爱错误页面。 The goal is that the site redirects with only a 404 status code (so not a 200, or a 302 redirect). 目标是网站仅重定向404状态代码(因此不是200或302重定向)。 I have tried in both the web config with: 我在web配置中尝试过:

<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. 这适用于具有.aspx文件扩展名的URL,但不适用于没有扩展名的URL。 Same with this approach in the global.asax 与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: 对此有相同的结果:(我最后的尝试是将此应用于Web配置:

<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! 提前致谢!

The following codes works with both .aspx and other file types: 以下代码适用于.aspx和其他文件类型:

Global.asax Global.asax中

void Application_Error(object sender, EventArgs e)
{
    var serverError = Server.GetLastError() as HttpException;
    if (serverError != null)
    {
        if (serverError.GetHttpCode() == 404)
        {
            Server.ClearError();
            Response.Redirect("~/NotFound.aspx?URL=" + Request.Url.ToString());
        }
        Response.Redirect("~/Default.aspx");
    }
}

Web.config Web.config文件

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

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/ 这是一篇关于管道模式及其差异的文章 - http://learn.iis.net/page.aspx/508/wildcard-script-mapping-and-iis-7-integrated-pipeline/

<system.webServer >
    <httpErrors errorMode="Custom">
      <remove statusCode="404" subStatusCode="-1" />
       <error statusCode="404" path="http://www.seair.co.in/Page-not-found.aspx" responseMode="Redirect" />
    </httpErrors>
</system.webServer>

use the code in your config and give complete path of error page 使用配置中的代码并提供错误页面的完整路径

For classic asp you can use this 对于经典的asp,你可以使用它

<system.webServer>
    <httpErrors>
      <clear />
      <error statusCode="404" subStatusCode="-1" path="/404.html" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM