In my web.config I have:
<customErrors mode="On" defaultRedirect="~/Error.aspx" redirectMode="ResponseRewrite">
<error statusCode="500" redirect="~/Error.aspx" />
<error statusCode="404" redirect="~/404.aspx" />
</customErrors>
Yet with these URL's, only the 2nd one with file extension defined works, the other one returns the blue screen 404 "Server Error in Application".
http://127.0.0.1/scirranew/invalidpath
http://127.0.0.1/scirranew/invalidpath.aspx
Any ideas? I'm on ASP.net 4, IIS 7.5.
If you want this to work, you need to configure IIS to pass all requests to the .NET engine. Your first example is not obviously asking for a .NET page, so it is not routed to the .NET engine.
What version of IIS and .NET are you using? This may help.
ASP.Net only gets invoked for file extensions registered to it in IIS. So when you go to invalidpath
IIS checks for a file matching that name, then a folder. If neither exists it invokes the 404 page designated in IIS.
Depending on your version of IIS you have a couple of different options. In IIS6 you have to manually set the 404 page from within the IIS manager. In IIS7 and greater you can use the web.config and changes the system.webServer
section:
http://www.iis.net/ConfigReference/system.webServer/httpErrors
I Have 2 configuration in the same web.config
<system.web>
<customErrors mode="On" defaultRedirect="/error.aspx" redirectMode="ResponseRedirect">
</customErrors>
</system.web>
you could use too customErrors mode="RemoteOnly"
And
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="401" subStatusCode="-1" />
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="401" path="/401.aspx" responseMode="ExecuteURL" />
<error statusCode="403" path="/403.aspx" responseMode="ExecuteURL" />
<error statusCode="404" path="/404.aspx" responseMode="ExecuteURL" />
<error statusCode="500" path="/500.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
Take a look at path="/404.aspx" is different to path="~/404.aspx" or path="404.aspx"
Also, I would recommend using plain html pages for error pages if possible as they are much more reliable. For example, if the .net session store is unavailable (e.g. session database down) then the error page will try and redirect to the error page an infinitum!
Html pages are less reliant on the .net subsystem so more likely to work when things start to fail.