IIS overriding custom 404 error page in ASP.NET

放肆的年华 提交于 2019-12-04 11:54:35

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/

The following codes works with both .aspx and other file types:

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

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

For classic asp you can use this

<system.webServer>
    <httpErrors>
      <clear />
      <error statusCode="404" subStatusCode="-1" path="/404.html" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>
shuaib
<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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!