How to add a default error page using httpErrors - asp.net

i have successfully added a custom 404 page. what I want to do is to create another custom error page that is displayed when there is any error other than 404. e.g. 500, 403 etc.
this is what I have right now in webconfig
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/404.aspx" responseMode="ExecuteURL"/>
</httpErrors>

Oh, my. I cannot believe I could not find a proper answer for this simple question! Nevertheless, after 2 hours of reading the docs and debugging, I found it.
<httpErrors errorMode="Custom" existingResponse="Auto" defaultResponseMode="ExecuteURL" defaultPath="/App/Error"> <!-- Do not include ~, this was my issue all long -->
<clear/> <!-- so that IIS provided error pages are skipped -->
<!-- add those which you like to provide a view of yours -->
<error path="/App/Http404" responseMode="ExecuteURL" statusCode="404"/>
<error path="/App/Http503" responseMode="ExecuteURL" statusCode="503"/>
</httpErrors>
Beaware that <httpErrors> configures IIS, while <customErrors> configures ASP.NET and some older versions of IIS (<=6?).

You can use customErrors in webconfig :
<customErrors mode="On" defaultRedirect="~/DefaultError.aspx?msg=SomeMessage">
<error statusCode="404" redirect="~/PageNotFound.html"/>
<error statusCode="403" redirect="~/AccessDenied.html"/>
</customErrors>

Related

Custom 404 not displaying

I have a webforms project that I've made a custom 404 page for.
I've written my redirect in my web.config but it doesn't seem to be working.
Here is the code
<customErrors mode="On" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="~/404.aspx"/>
</customErrors>
When I visit localhost:(port number)/about I'm taken to the about page. When I visit the same thing but spell 'about' incorrectly, I'm taken to the default 404 page
What am I missing?
Custom errors handles aspx pages. You can remove/comment your customErrors section and use httpErrors like this instead:
<system.webServer>
<httpErrors existingResponse="Replace" errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="404.aspx" responseMode="Redirect" />
</httpErrors>
</system.webServer>
Reference: HTTP Errors

I am using UrlRewritingNet.UrlRewrite dll, how to redirect to Error page when bad request get fired?

Currently I am using UrlRewritingNet.UrlRewrite dll for URL rewriting in Asp.Net #3.5.
Without using any special character it works fine ex. URL1.
But after giving special characters in URL it throws Bad Request error ex. URL2
URL1: http://www.example.com/search/0253
URL2: http://www.example.com/search/0253:0253
To handle this error, I want to redirect it to some other Error Page, How can I do this?
On IIS 7+ you can define error pages for all statuses, for 400 Bad Request :
<httpErrors>
<error statusCode="400" path="/bad-request.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL"/>
</httpErrors>
or trough IIS console go to the error pages and add custom error page for status code 400 :
The best way is by using Web.config file like
<system.web>
<!-- other system.web stuff -->
<customErrors defaultRedirect="/Error404.aspx" mode="On" redirectMode="ResponseRewrite">
<error redirect="/Error404.aspx" statusCode="404" />
</customErrors>
</system.web>
<system.webServer>
<!-- other system.webServer stuff -->
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/Error404.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
For more details click here

Asp.net custom 404 not working using Intelligencia rewriter

Web.config :
<customErrors mode="On">
<error statusCode="404" redirect="~/Page-Introuvable" />
</customErrors>
Rewriter.config :
<rewriter>
<rewrite url="~/Page-Introuvable" to="~/PageNotFound.aspx" />
</rewriter>
When typing this unexisting url :
http://example.com/qwerty.aspx
I get to see my error 404 custom page
When typing this :
http://example.com/qwerty (without .aspx)
I get server page 404 error
I'd like to know how to display my custom page in this case.
Thank you very much!
ANSWER
Added this to web.config section System.webServer :
<httpErrors errorMode="Custom">
<clear/>
<error statusCode="404" responseMode="Redirect" path="PageNotFound.aspx"/>
</httpErrors>
<customErrors> are just for extensions handled by asp.net (aspx, ashx...), for all others use <httpErrors>: http://www.iis.net/ConfigReference/system.webServer/httpErrors
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="ExecuteURL" defaultPath="/error.aspx?code=404">
<clear/>
<error statusCode="404" responseMode="ExecuteURL" path="/error.aspx?code=404"/>
</httpErrors>
</system.webServer>

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?

Implementing a Custom Error page on an ASP.Net website

I have an ASP.Net website and I want to use a custom error page. I put the following code in my web.config
<customErrors mode="On" defaultRedirect="~/error.aspx">
<error statusCode="404" redirect="~/error.aspx" />
</customErrors>
The problem is when i go to a URL that does not exist is still uses the 404 error page specified in IIS Manager.
Question: How can I make it use the error.aspx page I have created? Why do the settings in IIS Manager override the web.config?
Try this way, almost same.. but that's what I did, and working.
<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="apperror.aspx">
<error statusCode="404" redirect="404.aspx" />
<error statusCode="500" redirect="500.aspx" />
</customErrors>
</system.web>
</configuration>
or try to change the 404 error page from IIS settings, if required urgently.
There are 2 ways to configure custom error pages for ASP.NET sites:
Internet Information Services (IIS) Manager (the GUI)
web.config file
This article explains how to do each:
How To Set Up Custom Error Pages In IIS 7.5 With ASP.NET
The reason your error.aspx page is not displaying might be because you have an error in your web.config. Try this instead:
<configuration>
<system.web>
<customErrors defaultRedirect="error.aspx" mode="RemoteOnly">
<error statusCode="404" redirect="error.aspx"/>
</customErrors>
</system.web>
</configuration>
You might need to make sure that Error Pages in IIS Manager - Feature Delegation is set to Read/Write:
Also, this answer may help you configure the web.config file:
What is the difference between customErrors and httpErrors?
<customErrors defaultRedirect="~/404.aspx" mode="On">
<error statusCode="404" redirect="~/404.aspx"/>
</customErrors>
Code above is only for "Page Not Found Error-404" if file extension is known(.html,.aspx etc)
Beside it you also have set Customer Errors for extension not known or not correct as
.aspwx or .vivaldo. You have to add httperrors settings in web.config
<httpErrors errorMode="Custom">
<error statusCode="404" prefixLanguageFilePath="" path="/404.aspx" responseMode="Redirect" />
</httpErrors>
<modules runAllManagedModulesForAllRequests="true"/>
it must be inside the <system.webServer> </system.webServer>
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="your page" responseMode="Redirect" />
</httpErrors>
</system.webServer>
Is it a spelling error in your closing tag ie:
</CustomErrors> instead of </CustomError>?

Resources