Example of Configuration for <httpErrors> - asp.net

I have a Web Application in Asp.Net 4 running locally on IIS 7.
I need display a Custom Page (404) and an 500 instead for the defaults page for IIS.
Using this httpErrors in Web.Config
<system.webServer>
<httpErrors>
My Site is in
C:\inetpub\wwwroot\mysite\
My Custom Error page in:
C:\inetpub\wwwroot\mysite\ErrorPages\404.htm
C:\inetpub\wwwroot\mysite\ErrorPages\505.htm
I do not understand how it works. COuld you please provide me a sample of code?
Thanks

I solved my problem with this.
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode='-1' />
<remove statusCode="500" subStatusCode='-1' />
<error statusCode="404" path="/404.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL" />
<error statusCode="500" path="/500.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL" />
</httpErrors>
This needs to go in Web.config, under <configuration> > <system.webServer>
e.g.
<configuration>
<system.webServer>
<httpErrors ...>
// define errors in here ...
</httpErrors>
</system.webServer>
</configuration>

Here is an example, hope it helps
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="default.aspx">
<error statusCode="404" redirect="~/ErrorPages/404.htm"/>
<error statusCode="500" redirect="~/ErrorPages/505.htm"/>
</customErrors>
</system.web>
Edit for comments: Here's the example I think you need
<configuration>
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File" >
<remove statusCode="500" />
<error statusCode="500"
prefixLanguageFilePath="C:\Contoso\Content\errors"
path="500.htm" />
</httpErrors>
</system.webServer>
</configuration>
http://www.iis.net/ConfigReference/system.webServer/httpErrors/error

Related

ASP.NET error handling for 404 pages without extension

I have a website:Website Link with a problem, when it comes to extension less calls.
if you try something like:
This
I would like the user to go to:
here
but that is not what happens.
How can i change that?
Atm. i have this in my web.config:
<customErrors mode="On" defaultRedirect="/Error.aspx" redirectMode="ResponseRewrite">
<error statusCode="400" redirect="/Error.aspx?code=400" />
<error statusCode="403" redirect="/Error.aspx?code=403" />
<error statusCode="404" redirect="/Error.aspx?code=404" />
<error statusCode="500" redirect="/Error.aspx?code=500" />
</customErrors>
and
<system.webServer>
<httpErrors existingResponse="PassThrough" />
<modules runAllManagedModulesForAllRequests="true">
Some rewriting sends Error.aspx to "/findes-ikke"
After som more testing, with no luck, this is my entire web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation batch="false" targetFramework="4.7.1" debug="true" />
<customErrors mode="On" defaultRedirect="/Error.aspx" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="/Error.aspx?code=404" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/findes-ikke?code=404" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
</configuration>
You can try the following:
change the web.config line to:
<error statusCode="404" redirect="/findes-ikke?code=404" />
You can also try to add following in web.config to redirect all http requests:
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/findes-ikke?code=404" responseMode="ExecuteURL"/>
</httpErrors>
You can also do this to redirect all error requests. Make this change in the code you have given above:
<customErrors mode="On" defaultRedirect="/findes-ikke?code=404" redirectMode="ResponseRewrite">

web.config custom httpErrors not redirecting... "The page cannot be displayed because an internal server error has occurred." instead

I'm trying to get custom error pages work to no avail. This configuration is adapted from samples I've found online. Using IIS 7.0 hosted by GoDaddy.
The below code gives me "The page cannot be displayed because an internal server error has occurred." instead of redirecting.
<configuration>
<system.webServer>
<httpErrors>
<remove statusCode="401" subStatusCode="-1" />
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="401" path="http://www.mywebsite.com/error.html" responseMode="Redirect" />
<error statusCode="403" path="http://www.mywebsite.com/error.html" responseMode="Redirect" />
<error statusCode="404" path="http://www.mywebsite.com/error.html" responseMode="Redirect" />
<error statusCode="500" path="http://www.mywebsite.com/error.html" responseMode="Redirect" />
</httpErrors>
</system.webServer>
<system.web>
<customErrors mode="On"/>
</system.web>
</configuration>
This issue can sometimes happen on a shared hosting plan. See, https://forums.asp.net/t/1894946.aspx?Not+getting+custom+error+page, last post:
"You cannot modify the HTTP Error pages on Windows shared hosting plans outside of the 404 error page, which is why you are experiencing the issues"
So, if you have something like this:
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear />
<error statusCode="400" responseMode="Redirect" path="/Errors/404error.html" />
<error statusCode="403" responseMode="Redirect" path="/Errors/404error.html" />
<error statusCode="404" responseMode="Redirect" path="/Errors/404error.html" />
<error statusCode="500" responseMode="Redirect" path="/Errors/404error.html" />
</httpErrors>
You'll get the error "The page cannot be displayed because an internal server error has occurred." but if you change it to this:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" responseMode="Redirect" path="\Errors\404error.html"/>
</httpErrors>
It will work.
A full discussion of why this happens on GoDaddy can be found on this old forum thread here: https://web.archive.org/web/20130131011933/https://support.godaddy.com/groups/web-hosting/forum/topic/custom-http-error-pages-on-4gh-windows-shared-hosting/ .
There lots of other issues why your custom errors may not be working, but this particular one doesn't seem to be well documented, so I thought I'd post it here for others.

Custom error redirect on 404 code for non-aspx files

I added to my web.config:
<customErrors mode="RemoteOnly" defaultRedirect="~/Pages/Error/Error.aspx">
<error statusCode="404" redirect="~/Pages/Error/Error404.aspx"/>
</customErrors>
and it works fine for .aspx pages.
When I try to type: http://domainname.com/notExist, I get the default 404.
I tried the suggestions from:
404 Redirecting for non aspx pages
but nothing works for me.
Any ideas why?
I use .net 3.5, application pool pipeline mode is Integrated.
After adding <httpErrors> I get:
Try This, it will provide separate status. Then you can execute as per your requirement.
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/Pages/Error/Error.aspx">
<error statusCode="404" redirect="~/Pages/Error/Error404.aspx"/>
</customErrors>
</system.web>
<system.webserver>
<asp scriptErrorSentToBrowser="true"/>
<httpErrors errorMode="Detailed">
<error statusCode="401" path="/Pages/Error/Error.aspx" responseMode="Redirect" />
<error statusCode="403" path="/Pages/Error/Error.aspx" responseMode="ExecuteURL" />
<error statusCode="404" path="/Pages/Error/Error404.aspx" responseMode="ExecuteURL" />
<error statusCode="500" path="/Pages/Error/Error.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webserver>
You need to separate setting for other pages.
Try something like this.
<system.webserver>
<httpErrors errorMode="Custom" >
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="~/Pages/Error/Error404.aspx" />
</httpErrors>
</system.webserver>

Error in web.config redirect to error404 page

Getting internal server error... want to redirect page on error "404 Page not found".
<?xml version="1.0"?>
<configuration>
<system.web>
<urlMappings enabled="true">
<add url="~/Error404" mappedUrl="~/Error404.aspx"/>
</urlMappings>
<pages controlRenderingCompatibilityVersion="4.0"/>
<customErrors mode="on" defaultRedirect="~/Error404">
<error statusCode="404" redirect="~/Error404" />
<error statusCode="500" redirect="~/Error404" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/Error404" responseMode="ExecuteURL"/>
</httpErrors>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
Check this out:
Implementing a Custom Error page on an ASP.Net website
Also, it looks like you're missing ".aspx" at the end of your page paths.

Asp.net - Web.Config - Custom Errors

How can I set 404 and other error pages using web.config? I have tried adding following block in web.config.
<customErrors defaultRedirect="Forms/Errors/Page_404.aspx" mode="On">
<error statusCode="500" redirect="servererror.aspx" />
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="Forms/Errors/Page_404.aspx" />
</customErrors>
but still its showing default error page of IIS7. How to fix this?
I solved it myself. We need to add another section in web.config like below to make it work in IIS 7 / 7.5. For IIS 6 the one works which I mentioned in my question
<system.webServer>
...
<httpErrors errorMode="Custom" >
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" path="/404.aspx" responseMode="Redirect" />
<error statusCode="403" path="/403.aspx" responseMode="Redirect" />
<error statusCode="500" path="/500.aspx" responseMode="Redirect" />
</httpErrors>
...
</system.webServer>
Thanks to everyone who answered.
Try putting this in the system.webServer section of your Web.config
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
Try to add the "~/" before paths:
<customErrors defaultRedirect="~/Forms/Errors/Page_404.aspx" mode="On">
<error statusCode="500" redirect="~/servererror.aspx" />
<error statusCode="403" redirect="~/NoAccess.htm" />
<error statusCode="404" redirect="~/Forms/Errors/Page_404.aspx" />
</customErrors>
It looks like you're using a relative path there. Could that be the problem?
Try using Fiddler to see what page your browser is being redirected to.

Resources