3

In my application I have a custom error for catching all unexpected errors and a 404 for when a user tries to go to a page that does not exist. The issue is, in production I can navigate successfully to an error page either by typing the path to the url in the browser window or by misuse of application but I cannot do the same with the 404. Here is my setup.enter image description here

Skullomania
  • 2,225
  • 2
  • 29
  • 65

2 Answers2

3

Possibly include errorMode as "Custom" and responseMode as "File" if you are trying to redirect to a custom error file as shown below.

<httpErrors errorMode="Custom">
      <remove statusCode="404"/>
      <error statusCode="404" path="FileNotFound.html" responseMode="File"/>
  </httpErrors>
  • 1
    Welcome to SO, thank-you for contributing. Feel free to edit your answer at a later stage if you would like to expand on it or include some more detail that you might have left out. – AMAN77 Aug 18 '22 at 13:56
2

Error/NotFound will work instead of ~/Error/NotFound and custom errors should be enabled (depending on where you are making requests from):

<customErrors mode="On" defaultRedirect="Error">
    <error statusCode="404" redirect="Error/NotFound" />
</customErrors>

And also set httpErrors:

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="PassThrough">
        <clear />
        <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
    </httpErrors>
</system.webServer>

Beware that clear will remove static files for all errors. So consider to unlock an set defaultPath of httpErrors.

Andrii Litvinov
  • 12,402
  • 3
  • 52
  • 59
  • I tried this and it still does not work. It generates the same as the error shown above – Skullomania Apr 17 '17 at 19:03
  • Looks like it is because 404 is handled by IIS, not ASP.NET. Try also override `httpErrors`. – Andrii Litvinov Apr 17 '17 at 19:05
  • @Skullomania, I have updated my answer, hope it will help. That's how it was done on one of my previous projects. – Andrii Litvinov Apr 17 '17 at 19:13
  • I have added your updated suggestion to my webconfig in place of my custom errors as well as adding it below system.web with the same result. I have no clue what is going on. I can navigate to `notfound` the page in my local project but not in production – Skullomania Apr 17 '17 at 19:25
  • It replaces default IIS error pages. Can it be that this section is disabled on production somehow? You navigate to not found on local after changing `httpErrors` or you could do it from start? – Andrii Litvinov Apr 17 '17 at 19:30
  • Have a look at this question as well http://stackoverflow.com/questions/1888743/httperror-will-not-show-custom-error-pages – Andrii Litvinov Apr 17 '17 at 19:32
  • I did what it suggested and I am still having the same result. I am very close to scrapping this web.config and rewriting it from scratch just to see what the issue is. – Skullomania Apr 18 '17 at 00:38
  • Although this makes no sense, yesterday I used On instead of RemoteOnly and it started working. I am not sure why it started working because I tried that before as well. Thanks for the help! – Skullomania Apr 20 '17 at 13:14
  • Okay, good to know. It might be because the server and client was on the same machine. – Andrii Litvinov Apr 20 '17 at 13:20
  • 4
    It worked for me like: `existingResponse="Replace"` The full code : ` ` – mihkov Dec 04 '17 at 12:03