[404 Page Not Found] Error Handling

I have an existing ASP.Net application (not Core) with DotVVM being added to it. I have not been able to get the 404 error handling to work correctly.

I need to handle invalid page requests whether the request is for a DotVVM page (no extension) or an ASPX page or other extension.

Previous to adding DotVVM to the application, the following in the web.config worked.

<customErrors defaultRedirect="/Content/Errors/ErrorPage.aspx" mode="RemoteOnly" redirectMode="ResponseRewrite">
  <error statusCode="403" redirect="~/Content/Errors/UnauthorizedAccess.aspx" />
  <error statusCode="404" redirect="~/Content/Errors/PageNotFound.aspx" />
</customErrors>

After adding DotVVM to the application, the above no longer works, nor do any of the following.

<customErrors defaultRedirect="/Content/Errors/ErrorPage.aspx" mode="On" redirectMode="ResponseRewrite">
  <error statusCode="403" redirect="~/Content/Errors/UnauthorizedAccess.aspx" />
  <error statusCode="404" redirect="~/Errors/PageNotFound" />
</customErrors>
<customErrors mode="On" redirectMode="ResponseRewrite" >
    <error statusCode="404" redirect="/Errors/PageNotFound"/>
</customErrors>

What is the proper way to handle a 404 Page Not Found error in an existing ASP.Net (not Core) application?

It seems that you are using the old IIS system.web/customErrors section that was used in IIS 6 and very old versions of ASP.NET.

From IIS 7, there is the new system.webServer/httpErrors section that you need to use:

  <system.webServer>
      <httpErrors errorMode="Custom">
        <clear />
        <error statusCode="404" path="/Errors/PageNotFound" responseMode="ExecuteURL" />
      </httpErrors>
  </system.webServer>

Thank you, Tomas, for the information.

You are correct, I am working on a very old application. When I looked to apply your suggestion I noticed that we already had that in place. I spent the better part of a week trying to get it to work with lots of different combinations.

Finally I discovered that it worked on the servers (but still not on my local machine) when I used our original config settings but removing redirectMode="ResponseRewrite" from the CustomErrors line.

I can’t explain it but it’s working for now. It appears that I must continue to use the old error pages until the conversion is completed.

Maybe this will be helpful to others.