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?