Custom error in web.config not working for 404 - asp.net

I am trying to set up custom error for my website, which works fine on my local machine but it fails on the server. Below is my code.
<system.web>
<customErrors mode="On" defaultRedirect="~/Pages/Error.aspx">
<error statusCode="404" redirect="~/Pages/PageNotFound.aspx"/>
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="PassThrough"defaultResponseMode="ExecuteURL">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="~/Pages/PageNotFound.aspx" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
I am using IIS 8 on my local server and IIS 7 on the live server. Should I change this code in any way to make it work?
Also, when I try to access a page, eg: xyz.aspx, it does'nt show anything except for a white browser window.

Check if you have more then one web config Or Check your web config file is inside your project correctly.

Related

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.

Custom Error not working in actual website for 404

I am trying to setup custom error for my website which works fine on local-host mode="On".
But same fails on actual website.
Below is my code for web.config
<customErrors mode="RemoteOnly" defaultRedirect="~/error/Message.aspx?msg=The page you requested could not be found.">
<error statusCode="404" redirect="~/error/Default.aspx?PID=32"/>
<error statusCode="403" redirect="~/error/Forbidden.aspx"/>
<error statusCode="400" redirect="~/error/Error.aspx"/>
<error statusCode="500" redirect="~/error/InternalError.aspx"/>
</customErrors>
I am using IIS 7.5 on my local machine and same is being used on the web server.
Do i need to change any specific setting on Production server to make it work
Error message i am still getting on live website for 404
Not Found
The requested document was not found on this server.
Web Server at xyz.com
Final version with following code worked
...
<customErrors mode="RemoteOnly" defaultRedirect="~/error/Message.aspx?msg=The page you requested could not be found.">
<error statusCode="404" redirect="~/error/Default.aspx?PID=32"/>
<error statusCode="403" redirect="~/error/Forbidden.aspx"/>
<error statusCode="400" redirect="~/error/Error.aspx"/>
<error statusCode="500" redirect="~/error/InternalError.aspx"/>
</customErrors>
....
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/error/Default.aspx?PID=32" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
the web.config part seems to be ok, even if I would suggest static html for default error page redirection.
How do you test this issue on remote server?
By typing url xyz.com/xxxxxx where xxxxxx is page which not exist?
If yes, so is xxxxxx without file extension?
And does 404 redirect works if xxxxx ends with file extension which is one of website default page? e.g. xxxx.aspx
Try this post
asp.net custom error page and file without extension

DNN Custom Error Page Issue

I am working on DNN 7.0. I create a ErrorPage.aspx and set it as defaultredirect and another page 404error.aspx and set it with the status code 404.
now if i access the website like
www.abc.com/testing.aspx , and testing.aspx donot exists, it takes me to 404error.aspx page
then i try to access my website by
www.abc.com/testing , now it is showing that ugly asp.net error.
need some assistance what am i doing wrong, or what i am missing.
Thanks
Got it working.
In web.config implemented httpErrors and CustomErros tag
like
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/404Error.aspx" responseMode="ExecuteURL" />
</httpErrors>
and
<customErrors mode="On" defaultRedirect="/CustomErrorPage.aspx">
</customErrors>

ASP.Net Swallowing 404 Error

So I've set up a rule in IIS7 to redirect to a specific page on a 404 error.
This works great as long as I'm typing URLs such as:
www.abc.com/Test/
However as soon as I attempt to access a file that doesn't exist
www.abc.com/Test/Test.aspx
I get the ASP.Net error message:
Server Error in '/' Application.
The resource cannot be found.
How can I keep ASP.Net from swallowing the 404 error and overriding my settings in IIS7?
EDIT: Here's my web.config file. IIS added the system.webServer stuff not me.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
<system.webServer>
<httpErrors>
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/redir.aspx" responseMode="ExecuteURL" />
<error statusCode="403" prefixLanguageFilePath="" path="/redir.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
</configuration>
If you're in Classic pipeline mode, you may be able to use the "Invoke handler only if request is mapped to file" option. I had to turn it on for two .aspx mappings--I think one was 32 bit and one was 64.

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