2

I'm trying to show custom error pages in my ASP.NET MVC app. I understand that some errors are handles by ASP.NET and others by IIS.

The ASP.NET ones are handled and work fine

<customErrors mode="On" defaultRedirect="~/Error/Index"/>

When I request a URL that does not exist and references a static page like .html, I expect IIS to handle it.

<httpErrors errorMode="Custom">
      <remove statusCode="404"/>
      <error statusCode="404" path="http://localhost/MySite/404.html" responseMode="Redirect"/>
</httpErrors>

And that works. However I don't want to hard code http://localhost in there but make it a relative path.

So I tried

<httpErrors errorMode="Custom">
    <remove statusCode="404"/>
    <error statusCode="404" path="~/404.html" responseMode="Redirect"/>
</httpErrors>

However that keeps redirecting me (it appears) as the URL ends up being http://localhost/MySite/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/404.html

It also works if I do

<httpErrors errorMode="Custom">
    <remove statusCode="404"/>
    <error statusCode="404" path="404.html" responseMode="Redirect"/>
</httpErrors>

But of course only if the error is in http://localhost/MySite/doesnotexist.html root directory but not in any other like http://localhost/MySite/somedir/doesnotexist.html

If I change the responseMode to any of the other options it doesn't work at all, shows me the default IIS 404 page.

I'm deeply puzzled, what is causing the redirection loop?

1
  • Is path="~/404.html" just not getting the correct path? Ie the Web Application root?
    – Peete
    Sep 6, 2020 at 21:47

2 Answers 2

1

I've been able to replicate the behaviour you are describing. Mine was fixed when I changed the responseMode to file. I.e. like this

<httpErrors errorMode="Custom">
  <remove statusCode="404" />
  <error statusCode="404" path="404.html" responseMode="File" />
</httpErrors> 

But maybe the problem is because you have put your 404.html in the mysite folder. Assuming it has to stay there I think it should work if you do this

<httpErrors errorMode="Custom">
  <remove statusCode="404" />
  <error statusCode="404" path="/mysite/404.html" responseMode="Redirect" />
</httpErrors>   
1
  • Mysite is the IIS Application which can be customized so I cannot hard code that in either. But you are right that it would work, same as when using localhost/MySite/404.html. When I use file it doesn't pick it up at all, it's like not specifying any httpErrors at all
    – Peete
    Sep 6, 2020 at 21:46
1

The path attribute specifies the file path or URL that is served in response to the HTTP error specified by the statusCode and subStatusCode attributes. If you choose the File response mode, you specify the path of the custom error page. If you choose the ExecuteURL response mode, the path has to be a server relative URL (for example, /404.htm). If you choose the Redirect response mode, you have to enter an absolute URL (for example, www.contoso.com/404.htm).

The responseMode attribute specifies how custom error content is returned. The responseMode attribute can be one of the following possible values. Redirects client browsers to a the URL specified in the path attribute that contains the custom error file. If responseMode is set to Redirect, the path value has to be an absolute URL.

2
  • Thanks, yes, I read up on it and that's how it is. However I wonder if there is a way to enter a path relative to the root of the application, rather than the root of the server, without specifying the application name/folder
    – Peete
    Sep 8, 2020 at 20:29
  • 1
    This seems impossible, you need to choose which path to use according to responseMode, there are only three responseMode, and there is no path relative to the root of the application.
    – samwu
    Sep 10, 2020 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.