IIS Overrides Custom 404 Error Page in ASP.NET

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

http: //name/something.aspx

It works as intended. But if the user enters:

http: // name / NotAFile

without .aspx, then IIS7 takes matters into my own hands, and I get the excellent error page that IIS7 comes with. The goal is that the site only redirects a 404 status code (not 200 or a 302 redirect). I tried in web configuration with:

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

This works for a URL with the extension .aspx, but not for the extension. Same thing with this approach in 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");
    }
}

:( , -:

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

, ... ! !

+3
4

.aspx, :

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>
+4

asp

<system.webServer>
    <httpErrors>
      <clear />
      <error statusCode="404" subStatusCode="-1" path="/404.html" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>
0
source
<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 configuration and give the full path to the error page

0
source

Source: https://habr.com/ru/post/1793163/


All Articles