Home > front end >  IIS custom 404 page
IIS custom 404 page

Time:11-11

As stated in the title i need to configure a custom web page for 404 error in my site (made with ASP.NET) and hosted on IIS.

To do so, i configure web.config as follow :

<httpErrors>
  <remove statusCode="404" subStatusCode="-1" />
  <error statusCode="404" prefixLanguageFilePath="" path="/mycustompath/404.html" responseMode="ExecuteURL" />
</httpErrors>

Everything work just fine, if i type :

https://mydomain/path/page.aspx__somethingwrong__

i correctly go to my custom 404.html Same happen if i put some random character instead of file

But if i mess up with the path, or type an unexisting .aspx file, then IIS redirect me to default 404 page. And is not clear to me why.

Example of url that does not work :

https://mydomain/invalid_path/page.aspx

https://mydomain/path/fictional_page.aspx

CodePudding user response:

You can add errorMode="Custom" and existingResponse="Replace" in the web.config file to solve your problem.

<httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" prefixLanguageFilePath="" path="/MyErrorPages/404.html" responseMode="ExecuteURL" />
</httpErrors>

existingResponse specifies what happens to an existing response when the HTTP status code is an error, It also has 3 options:

  • auto(Default) : Leaves the response untouched only if the SetStatus flag is set.
  • Replace : Replaces the existing response even if the SetStatus flag is set. (means using custom page anyway)
  • Passthrough : Leaves the response untouched if an existing response exists.
  • Related