HttpError iis config throws exception when default path is added - asp.net

I have this config which works and redirects the following errors correctly
<httpErrors errorMode="Custom"
existingResponse="Replace"
defaultResponseMode="ExecuteURL" >
<remove statusCode="403" />
<remove statusCode="404" />
<remove statusCode="500" />
<error statusCode="403" responseMode="ExecuteURL" path="/Error/AccessDenied" />
<error statusCode="404" responseMode="ExecuteURL" path="/Error/PageNotFound" />
<error statusCode="500" responseMode="ExecuteURL" path="/Error/ApplicationError" />
</httpErrors>
But when I add the following default path to try to add a catch all
<httpErrors errorMode="Custom"
existingResponse="Replace"
defaultResponseMode="ExecuteURL"
defaultPath="/Error/ApplicationError">
The server throws a web.config error
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
Module CustomErrorModule
Now this directly contradicts the documentation on msdn
Any help would be greatly appreciated!!

Using of defaultPath attribute prevents using of path attribute in your error nodes. So below configuration will work (but of course it will show the same error page for all HTTP errors defined here):
<httpErrors errorMode="Custom" existingResponse="Replace"
defaultResponseMode="ExecuteURL" defaultPath="/Error/ApplicationError">
<remove statusCode="403" />
<remove statusCode="404" />
<remove statusCode="500" />
<error statusCode="403" responseMode="ExecuteURL" />
<error statusCode="404" responseMode="ExecuteURL" />
<error statusCode="500" responseMode="ExecuteURL" />
</httpErrors>
Related doc: https://msdn.microsoft.com/en-us/library/ms690576(v=vs.90).aspx

You cannot override httpErrors "defaultPath" attribute in IISExpress because of applicationhost.config locked that attribute:
<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">
You can read more about it here: https://support.microsoft.com/en-us/kb/942055 This problem can occur:
when the specified portion of the IIS configuration file is locked at
a higher configuration level. To resolve this problem, unlock the
specified section, or do not use it at that level. For more
information on configuration locking, see How to Use Locking in IIS
7.0 Configuration.

This is becuase IIS by default (Ive just discovered this with IIS 10) at a server level locks defaultPath.
The error is saying some parent web.config attribute has been locked so you're not allowed to overwrite it.
The way to change this is to
Open IIS
Select the top level node in the tree (Your server/computer name most likely)
Click the 'Configuration Editor' icon in the last row.
Enter 'system.webServer/httpErrors' into the section dropdown at the top
Right click the defaultPath
Go to the 'defaultPath' attribute > sub menu
Click Unlock attribute
Click Apply Changes in the top right
I'd generally recommend against this though, as you'll have to do this on every server you deploy the site to.
(and I'm also not sure how something like Azure Web apps that dont give you this level of access handle it)

Try defaultPath="~/Error/ApplicationError" with ~.

Related

IIS custom 404 page

As stated in the title i need to configure a custom web page for 404 error in my site (made with ASP.NET) and hosted on IIS.
To do so, i configure web.config as follow :
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/mycustompath/404.html" responseMode="ExecuteURL" />
</httpErrors>
Everything work just fine, if i type :
https://mydomain/path/page.aspx__somethingwrong__
i correctly go to my custom 404.html
Same happen if i put some random character instead of file
But if i mess up with the path, or type an unexisting .aspx file, then IIS redirect me to default 404 page. And is not clear to me why.
Example of url that does not work :
https://mydomain/invalid_path/page.aspx
https://mydomain/path/fictional_page.aspx
You can add errorMode="Custom" and existingResponse="Replace" in the web.config file to solve your problem.
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/MyErrorPages/404.html" responseMode="ExecuteURL" />
</httpErrors>
existingResponse specifies what happens to an existing response when the HTTP status code is an error, It also has 3 options:
auto(Default) : Leaves the response untouched only if the SetStatus
flag is set.
Replace : Replaces the existing response even if the SetStatus flag
is set. (means using custom page anyway)
Passthrough : Leaves the response untouched if an existing response
exists.

Can I tell IIS 8.5 to return a 404 when using httpErrors?

I'm working on a (Web Forms) site running on IIS 8.5 and am currently setting up error handling for 404s and 500s.
I already have the system.web > customErrors setup, but since we're running in Integrated mode/newer version of IIS it's pulling from system.webServer > httpErrors.
Using the IIS GUI I opted to replace the standard IIS error pages with a static HTML file, on the site:
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath=""
path="/problem.html" responseMode="ExecuteURL" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" prefixLanguageFilePath=""
path="/problem.html" responseMode="ExecuteURL" />
</httpErrors>
Unfortunately, while the correct error page is displayed, instead of a returning a 404 a 200 is returned, and a 302 followed by a 200 is returned for 500 errors.
For older versions of IIS, or those not running in Integrated mode, you could set redirectMode="ResponseRewrite" on the customErrors element and that would resolve this issue.
I've tried setting existingResponse on httpErrors to all three available options, as well as tried updating setting responseMode="File", but these changed nothing.
Is there any way to do something similar for the httpErrrors element? Or am I stuck with pointing the errors to an ASP.NET page and returning the appropriate error from there?
Thanks!
To solve this, you can set this configuration into the <httpErrors> in your WebConfig file. Like this:
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear/>
<error statusCode="404" path="/WebForms/Index.aspx" responseMode="File"/>
</httpErrors>
<system.webServer/>
The responseMode=File will preserve the original error code, and show the static page.

How To : Dnn Custom Error Pages

Default error pages may expose a lot of information, Like the language and the server you use, and such Info. could be used by malicious users to attack your site and your server.
Also default error pages look very ugly and outdated design, which may harm your brand.
So It's always recommended to use custom error pages..
So, How Could I display a custom error page in DNN when error occurs ?
1.To Disable DNN Error Handling, and Let Errors Bubble..
Host Settings >> Appearance >> Un-Check "Use Custom Error Messages? "
2.To Redirect Users To, Can be aspx page, but I prefer HTML
Then Create HTML Page at the root with Style
3.in Enable CustomErrors in Web.Config :
<customErrors mode="On">
<error statusCode="500" redirect="/MyCustomError.html"/>
<error statusCode="404" redirect="/MyCustomError.html"/>
<error statusCode="403" redirect="/MyCustomError.html"/>
</customErrors>
Also You can Override IIS Error Pages and Redirect to your Custom Error Pages using the below web.config snippet
<httpErrors>
<remove statusCode="404" subStatusCode="-1"/>
<error statusCode="404" prefixLanguageFilePath="" path="/MyCustomError.aspx" responseMode="ExecuteURL"/>
<remove statusCode="403" subStatusCode="-1"/>
<error statusCode="403" prefixLanguageFilePath="" path="/MyCustomError.html" responseMode="ExecuteURL"/>
</httpErrors>

how do i specify a custom error page without setting defaultPath on httpErrors in the web.config?

I'm currently working on deploying a asp.net website to a shared hosted environment, and this works as expected (404 pages render)
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/file-not-found.htm" responseMode="ExecuteURL" />
</httpErrors>
but this doesn't (500 internal server error)
<httpErrors errorMode="Custom" defaultPath="error.htm" defaultResponseMode="ExecuteURL">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/file-not-found.htm" responseMode="ExecuteURL" />
</httpErrors>
I asked to take a look at the applicationHost.config and it has:
<httpErrors errorMode="Custom" defaultPath="C:\inetpub\custerr\en-US\SSLRedirect.htm" lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">
So I'm assuming that the defaultPath as a locked attribute is the thing that's causing issues.
Because this is being deployed to a shared environment, the hosting company is not willing to modify the applicationHost.config to remove the lock on defaultPath - so is there any way to specify a default error page without unlocking the defaultPath attribute?
In global.asax you could just implement Application_Error() and redirect to wherever you want?

HttpError will not show custom error pages

I've got this in the web.config:
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/Error/NotFound.aspx" responseMode="Redirect" />
<error statusCode="500" prefixLanguageFilePath="" path="/Error/ServerError.aspx" responseMode="Redirect" />
</httpErrors>
But IIS still shows the built in error page.
Any ideas?
You may also need to set the existingReponse attribute in the httpErrors element like this:
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear />
<error statusCode="404" prefixLanguageFilePath="" path="/ErrorHandler.aspx" responseMode="ExecuteURL" />
<error statusCode="500" prefixLanguageFilePath="" path="/ErrorHandler.aspx" responseMode="ExecuteURL" />
</httpErrors>
This is how I am using it and it works to me, it looks pretty similar except for the subStatusCode directives and the ExecuteURL.
<httpErrors>
<!--Remove inherited 500 error page setting -->
<remove statusCode='500' subStatusCode='-1'/>
<!--Override the inherited 500 error page setting with the 'My500.html' as its path-->
<error statusCode='500' subStatusCode='-1' prefixLanguageFilePath='' path='/My500.html' responseMode='ExecuteURL'/>
</httpErrors>
If you are using ExecuteURL, the custom error page path must be in the same application pool as the application itself.
For architectural reasons, IIS 7.0 can only execute the URL if it is located in the same Application Pool. Use the redirect feature to execute a Custom Error in a different Application Pool.
It seems as though you are using a server relative URL, try setting responseMode="ExecuteURL", from MSDN.
ExecuteURL
Serves dynamic content (for example,
an .asp file) specified in the path
attribute for the custom error. If
responseMode is set to ExecuteURL, the
path value has to be a server relative
URL. The numeric value is 1.
Redirect
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.
The numeric value is 2.
Ensure that you have the proper feature setting for the Error Page redirection in IIS. To check this, from the Error Pages page in IIS Manager, click Edit Feature Settings and make sure Custom error pages is checked if you are testing the redirects from the web server itself. If you are testing remotely, you can leave Detailed errors for local requests and custom error pages for remote requests is checked. This appears to be the default option in my test environment.

Resources