I'm trying to set a custom 404 error page for my web application. The trouble is that this application will be deployed to a number of different environments. Sometimes it will be in a virtual directory and sometimes it won't.
I have the error page in a directory called ErrorPages and have set up my config like this:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="/VirtualDir/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
The trouble is when I deploy this to the root of a web site, the /VirtualDir
part needs to be removed. If I remove it before deployment then I need to add it back in when deploying to a virtual directory. Is there any way I can set the path to be relative to the virtual directory and not to the site?
I have tried using a ~
, but that does not work either, like this:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="~/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
You could use web.config transforms to set the path per environment:
web.config
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="/VirtualDir/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
web.Release.config
<httpErrors>
<error statusCode="404" path="/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>