Skip CustomErrors and go to HttpErrors - asp.net

I have web.config settings like this
<customErrors mode="On" >
<error statusCode="404" redirect="~/404" />
<error statusCode="403" redirect="~/500" />
<error statusCode="500" redirect="~/500" />
</customErrors>
and for httperrors
<httpErrors errorMode="Custom" >
<error statusCode="400" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\bad_request.html" />
<remove statusCode="401" subStatusCode="-1" />
<error statusCode="401" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\unauthorized.html" />
<remove statusCode="403" subStatusCode="-1" />
<error statusCode="403" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\forbidden.html" />
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\not_found.html" /><remove statusCode="405" subStatusCode="-1" />
<error statusCode="405" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\method_not_allowed.html" /><remove statusCode="406" subStatusCode="-1" />
<error statusCode="406" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\not_acceptable.html" />
<error statusCode="407" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\proxy_authentication_required.html" /><remove statusCode="412" subStatusCode="-1" />
<error statusCode="412" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\precondition_failed.html" />
<error statusCode="414" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\request-uri_too_long.html" /><error statusCode="415" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\unsupported_media_type.html" />
<remove statusCode="500" subStatusCode="-1" /><error statusCode="500" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\internal_server_error.html" />
<remove statusCode="501" subStatusCode="-1" /><error statusCode="501" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\not_implemented.html" />
<remove statusCode="502" subStatusCode="-1" /><error statusCode="502" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\bad_gateway.html" />
<error statusCode="503" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\maintenance.html" />
</httpErrors>
On 404 Error. Skipping 404 page and goes to .aspxerrorpath= page.aspx
and writing
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
I want to show my customized 404, 500 error page. What is the wrong

In short, in production it is possible to completely get rid of the "system.web -> customErrors" section, which is almost kept for the compatibility purposes (check the What is the difference between customErrors and httpErrors? thread to learn more), and where you also specify redirects. Then, specify the necessary redirects and custom error pages within the "system.webServer -> httpErrors" section only. Make sure that these HTML files exist on the web server and are available via the relative paths (or these pages exist inside one of a parent web app on the same web server, so that these "errors" settings are inherited).
Solution Explorer:
~/YourWebApp
...
not_found.html
Web.config
Web.config (for the 404 status code error):
<!--Dev: For Visual Studio Mode-->
<system.web>
<customErrors mode="On">
<error statusCode="404" redirect="~/not_found.html" />
</customErrors>
</system.web>
<!--Prod: For IIS Mode-->
<system.webServer>
<httpErrors errorMode="Custom">
<!--<error statusCode="404" path="C:\Inetpub\vhosts\domain.win2012.tld\error_docs\not_found.html" />-->
<error statusCode="404" path="~/not_found.html" responseMode="Redirect" />
</httpErrors>
</system.webServer>

Related

ASP page error even custom error mode "ON"

I've got custom error mode "On" in my web.config. However, the ASPX error page still shows for certain kinds of particular URL
Not OK
https://example.com/*~1*%5ca.aspx <-- Return 404 - File or directory not found.
OK
https://example.com/abc.aspx <-- can redirect to custom page
I'm using Windows Server 2012 Azure. I disabled the NtfsDisable8dot3NameCreation in the registry, but still no luck. What can be the root cause?
My web config
<customErrors mode="On" defaultRedirect="~/error/Error403.html">
<error statusCode="400" redirect="~/error/Error400.html" />
<error statusCode="401" redirect="~/error/Error400.html" />
<error statusCode="402" redirect="~/error/Error400.html" />
<error statusCode="403" redirect="~/error/Error403.html" />
<error statusCode="404" redirect="~/error/Error404.html" />
<error statusCode="500" redirect="~/error/Error500.html" />
<error statusCode="501" redirect="~/error/Error501.html" />
<error statusCode="502" redirect="~/error/Error502.html" />
<error statusCode="503" redirect="~/error/Error503.html" />
<error statusCode="504" redirect="~/error/Error504.html" />
</customErrors>
<httpErrors errorMode="Custom" existingResponse="Auto" defaultResponseMode="ExecuteURL">
<remove statusCode="400" subStatusCode="-1"/>
<remove statusCode="401" subStatusCode="-1"/>
<remove statusCode="402" subStatusCode="-1"/>
<remove statusCode="403" subStatusCode="-1"/>
<remove statusCode="404" subStatusCode="-1"/>
<remove statusCode="500" subStatusCode="-1"/>
<remove statusCode="501" subStatusCode="-1"/>
<remove statusCode="502" subStatusCode="-1"/>
<remove statusCode="503" subStatusCode="-1"/>
<remove statusCode="504" subStatusCode="-1"/>
<!--<error statusCode="500" path="error\InternalServerError.htm"/>
<error statusCode="404" path="error\PageNotFound.htm"/>-->
<error statusCode="400" prefixLanguageFilePath="" responseMode="ExecuteURL" path="/error/Error400.html" />
<error statusCode="401" prefixLanguageFilePath="" responseMode="ExecuteURL" path="/error/Error400.html" />
<error statusCode="402" prefixLanguageFilePath="" responseMode="ExecuteURL" path="/error/Error400.html" />
<error statusCode="403" prefixLanguageFilePath="" responseMode="ExecuteURL" path="/error/Error400.html" />
<error statusCode="404" prefixLanguageFilePath="" responseMode="ExecuteURL" path="/error/Error404.html" />
<error statusCode="500" prefixLanguageFilePath="" responseMode="ExecuteURL" path="/error/Error500.html" />
<error statusCode="501" prefixLanguageFilePath="" responseMode="ExecuteURL" path="/error/Error501.html" />
<error statusCode="502" prefixLanguageFilePath="" responseMode="ExecuteURL" path="/error/Error502.html" />
<error statusCode="503" prefixLanguageFilePath="" responseMode="ExecuteURL" path="/error/Error503.html" />
<error statusCode="504" prefixLanguageFilePath="" responseMode="ExecuteURL" path="/error/Error504.html" />
</httpErrors>
I think you should get 400 error more than 404 error because "*" is not a valid request.path for .NET runtime and the exception was thrown from CLR. That's why IIS custom error page would never work.
Please try to set requestPathInvalidCharacters to null or manual specify invalid characters for your runtime.
<system.web>
<compilation targetFramework="4.8" />
<httpRuntime requestPathInvalidCharacters=""/>
<customErrors mode="RemoteOnly">
<error redirect="https://www.jokiesd.com/404.aspx" statusCode="404" />
</customErrors>
</system.web>
Edit:
I can' reproduce this issue on my 2012 server. Have you tried to replace the URL to abosolute URL?

custom httpError is throwing 500 internal server error in WCF service

I am trying to add my custom error handling for http errors that are thrown after requests to my WCF API.
I have added the below in my web.config:
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="10000" />
</requestFiltering>
</security>
<httpErrors errorMode="Custom" >
<remove statusCode="404" subStatusCode='-1' />
<error statusCode="404" prefixLanguageFilePath="" path="/GlobalHttpError.aspx" responseMode="ExecuteURL" />
<remove statusCode="401" subStatusCode='-1' />
<error statusCode="401" prefixLanguageFilePath="" path="/GlobalHttpError.aspx" responseMode="ExecuteURL" />
<remove statusCode="501" subStatusCode='-1' />
<error statusCode="501" prefixLanguageFilePath="" path="/GlobalHttpError.aspx" responseMode="ExecuteURL" />
<remove statusCode="411" subStatusCode='-1' />
<error statusCode="411" prefixLanguageFilePath="" path="/GlobalHttpError.aspx" responseMode="ExecuteURL" />
<remove statusCode="403" subStatusCode='-1' />
<error statusCode="403" prefixLanguageFilePath="" path="/GlobalHttpError.aspx" responseMode="ExecuteURL" />
</httpErrors>
I have a method that uploads files so I am trying to generate a custom error page once the file size exceeds the maxAllowedContentLength.
Whenever I add this piece of code to my web.config and I try to test the method from postman I get 500 error:
The page cannot be displayed because an internal server error has occurred.
What is this error and how can I resolve it?
Update
I tried also the below:
<httpErrors errorMode="Custom" >
<remove statusCode="404" subStatusCode='13' />
<error statusCode="404" subStatusCode='13' prefixLanguageFilePath="" path="/GlobalHttpError.aspx" responseMode="ExecuteURL" />
</httpErrors>
But still not working
Even if I add this only:
<httpErrors errorMode="Detailed"></httpErrors>
Also same error is thrown when maxAllowedContentLength is exceeded.

httpErrors not working on localhost in web.config

I have added following in my webconfig. It works when site is hosted to IIS but not when I run locally. It gives me error of wrong web.config.
<system.webServer>
<httpErrors errorMode="Custom" defaultPath="ShowError.aspx" defaultResponseMode="ExecuteURL">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="401" subStatusCode="-1" />
<remove statusCode="405" subStatusCode="-1" />
<remove statusCode="406" subStatusCode="-1" />
<remove statusCode="412" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<remove statusCode="501" subStatusCode="-1" />
<remove statusCode="502" subStatusCode="-1" />
</httpErrors>
</system.webServer>
The defaultPath attribute is locked by default, which causes the 500.19 error. You can either:
Unlock defaultPath by removing it from your applicationHost.config, which I do not recommend because then you'll have to configure this on every developer and production server.
Remove defaultPath and defaultResponseMode from <httpErrors> and specify path and responseMode on each <error>, like so:
<httpErrors errorMode="Custom">
<clear />
<error statusCode="404" responseMode="ExecuteURL" path="ShowError.aspx" />
<error statusCode="403" responseMode="ExecuteURL" path="ShowError.aspx" />
<error statusCode="401" responseMode="ExecuteURL" path="ShowError.aspx" />
<error statusCode="405" responseMode="ExecuteURL" path="ShowError.aspx" />
<error statusCode="406" responseMode="ExecuteURL" path="ShowError.aspx" />
<error statusCode="412" responseMode="ExecuteURL" path="ShowError.aspx" />
<error statusCode="500" responseMode="ExecuteURL" path="ShowError.aspx" />
<error statusCode="501" responseMode="ExecuteURL" path="ShowError.aspx" />
<error statusCode="502" responseMode="ExecuteURL" path="ShowError.aspx" />
</httpErrors>
It might seem excessive to specify each status code separately, but there is no other way when using httpErrors. I would recommend you to add 400 (Bad Request).
Some side notes:
You can omit subStatusCode="-1" because -1 is the default value of subStatusCode.
If you're going to remove/replace all default IIS error pages, you might as well use <clear />.
You might want to remove errorMode="Custom" when you've got your error pages working, because this will hide error details from developers, slowing down productivity while debugging. A good error page is only returned for remote requests, which is what the default value (errorMode="DetailedLocalOnly") will do.
If you can't get the error pages to work, try responseMode="Replace". You also want to avoid using this in developer environments for the same reasons as the above.
If you have custom error pages, try this:
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly" existingResponse="Auto">
<remove statusCode="403" />
<remove statusCode="404" />
<remove statusCode="500" />
<error statusCode="403" responseMode="ExecuteURL" path="/Error/AccessDenied" />
<error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
<error statusCode="500" responseMode="ExecuteURL" path="/Error/InternalServerError" />
</httpErrors>
If you want custom error pages on local, you can change DetailedLocalOnly to Custom.

404 Custom is blank with asp extension

My 404 Custom setup shows up as a blank page if it has an asp extension. If I change the extension of the custom error page to .html, the custom error page shows correctly.
this is my web.config file
<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL" existingResponse="Auto">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" subStatusCode="100" prefixLanguageFilePath="" path="/error_pages/error500.asp" responseMode="ExecuteURL" />
<error statusCode="404" subStatusCode="-1" prefixLanguageFilePath="" path="/error_pages/error404.asp" responseMode="ExecuteURL" />
</httpErrors>
I cant figure this out... driving me nuts
If you like to redirect when the error happens, use...
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/time.asp" responseMode="Redirect" />
</httpErrors>
You may also hide the redirection, by using...
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/time.asp" responseMode="ExecuteURL" />
</httpErrors>
You can also use Custom Errors (of ASP.NET) like so...
<system.web>
<customErrors mode="On">
<error redirect="time.asp" statusCode="404" />
</customErrors>
</system.web>

How to redirect all httpErrors to custom url?

These are the codes in web.config:
<system.web>
<customErrors mode="Off" >
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear />
<error statusCode="404" prefixLanguageFilePath="" path="/ResourceNotFound" responseMode="ExecuteURL" />
<error statusCode="500" prefixLanguageFilePath="" path="/ResourceNotFound" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
The above settings will redirect httpError of 404 and 500 only.
But instead of manually add all the error code of 400, 401, 403....etc..etc...
Can we just set it redirect all errors to the same url without typing all the error code?
<error statusCode="400" .....
<error statusCode="401" .....
<error statusCode="403" .....
<error statusCode="404" .....
<error statusCode="xxx" ....
The httpErrors section has defaultPath attribute.
<system.webServer>
<httpErrors defaultPath="Error.html" defaultResponseMode="File">
<clear />
</httpErrors>
</system.webServer>
http://www.iis.net/configreference/system.webserver/httperrors
However, I don't use it, because defaultPath is locked in IIS Express by default. Need to edit %homepath%\Documents\IISExpress\config\applicationHost.config to unlock it.
<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">
<!-- ... -->
</httpErrors>
try this,
add in web.config file.
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File" >
<remove statusCode="500" />
<error statusCode="500" prefixLanguageFilePath="C:\Contoso\Content\errors"
path="500.htm" />
</httpErrors>
</system.webServer>
and
<httpErrors existingResponse="Replace" defaultResponseMode="ExecuteURL" errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
<remove statusCode="401" />
<error statusCode="401" path="/Account/Login.aspx" responseMode="ExecuteURL"/>
<remove statusCode="501"/>
<error statusCode="501" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
<remove statusCode="411"/>
<error statusCode="411" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
<remove statusCode="403"/>
<error statusCode="403" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
</httpErrors>
and more about this http://www.iis.net/configreference/system.webserver/httperrors

Resources