404 error page not showing - asp.net

I've got this in my web.config and it's being hosted by the DiscountASP.net ISP
<customErrors mode="On" defaultRedirect="">
<error statusCode="404" redirect="404.aspx"/>
<error statusCode="500" redirect="404.aspx"/>
</customErrors>
I am hosting the site on DiscountASP.net and they also tell you to config it this way. I'm using Enterprise Library but I don't think that should make a difference. I don't believe I need to config anythign for a 404 in EL.
When my page loads with an error, my 404.aspx doesn't show and I get the default custom errors off message. I do not know why I don't get my 404.aspx page showing and get this instead:
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "Off".
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's configuration tag to point to a custom error page URL.

Have you tried using a relative path to see if that makes a difference?
<error statusCode="404" redirect="~/404.aspx"/>
Alternately, try an absolute path:
<error statusCode="404" redirect="http://www.domain.com/errors/404.aspx"/>
EDIT: As others have pointed out, and based on your comment to another answer, the 404 error page should be displayed when someone navigates to a page that doesn't exist, whereas general errors on a page could be captured by the defaultRedirect. So if you're testing the 404 then make sure you're testing with a nonexistent page. To test the defaultRedirect then have one of your pages throw an exception etc.

Is it possible that you are overriding this in another web.config, say in a sub folder? Or we can go the other way. Are you sure this is in the application root?

Is there any reason why you don't want to use the defaultRedirect for 500 errors?

You are FTP-ing the files correct? Try uploading the file as a binary file instead of text. You might be suffering from an encoding problem. I'm willing to bet right now that nothing you are doing in your web.config is working.

I found a solution here.
The real catch was using this:
Response.TrySkipIisCustomErrors = true;

Related

Kentico/ASP.NET ResponseRedirect still keeps initial URL

I have my web.config setup as follows:
<customErrors mode="RemoteOnly" redirectMode="ResponseRedirect" defaultRedirect="~/SpecialPages/PageNotFound.aspx">
<error statusCode="404" redirect="~/SpecialPages/PageNotFound.aspx" responseMode="Redirect"/>
</customErrors>
But when a user goes to mysite.com/gibberish, my 404 page shows up, but the url stays as mysite.com/gibberish, but I want it to say mysite.com/SpecialPages/PageNotFound. Is there somethng else I am missing?
I'm using Kentico10 CMS if that makes any difference but have been following their instructions too. Seems like I'm missing something server side.
Have you specified Page not found in Kentico? If so, remove that configuration beacuse you don't want Kentico to handle error for you and then your custom errors in web.config should work as you set them up as in any other project.
In case anybody is reading I got an answer from kentico themselves:
This behavior is correct from SEO point of view. Previously we had the
behavior you wanted but it was really bad for SEO to do a redirection
so it was changed and just the 404 status code is returned and the URL
is the same. If you want to change this behavior, you can create
custom event handler and in the request end event check the status
code and if it is 404, do a redirection - but this can harm your SEO
rankings.
https://docs.kentico.com/k11/custom-development/handling-global-events/reference-global-system-events#Reference-Globalsystemevents-RequestEvents

customError on web.config-originated Server Error

NOTE
I've determined the the problem I'm describing below is specific to an error enountered loading DLL files specified in the web.config file. I would like to present a user-friendly error even in the case of web.config errors.
END NOTE
When my ASP.Net application encounters a server error I would like it to present a custom error message to the user instead of the following default scary message.
Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>
I wrote a very simple HTML page and put it in the root of my application. It is called MaintenancePage.htm.
I've set my web.config file to the following:
<customErrors mode="RemoteOnly" defaultRedirect="MaintenancePage.htm">
<error statusCode="404" redirect="PageNotFound.aspx" />
</customErrors>
I've also tried ~/MaintenancePage.htm and http://[mysite]/MaintenancePage.htm. None of those options seem to work.
The way I'm testing this is to rename a DLL my project depends upon and then loading up the site in a web browser. I would expect that since there is an error and a defaultRedirect set there should be no problem showing the error page, however, I'm obviously wrong.
I've search around concerning this problem and it seem that most people are trying to redirect to an aspx page, and are encountering error with doing that. Many even report that they can't get the aspx page to load as a defaultRedirect, but they can get an html page to load.
What could I be doing wrong here?
I should note that I'm doing my testing from a different network outside the company firewall, so changing RemoteOnly to On is not the issue per the documentation. In testing changing RemoteOnly to On had no effect, as expected.
Change RemoteOnly to On.
RemoteOnly:
Specifies that custom errors are shown only to remote clients and
ASP.NET errors are shown to the local host
Also, your URL may be absolute or relative.
Read more about the customErrors settings
To watch it work locally:
<customErrors mode="On" defaultRedirect="/MaintenancePage.htm">
<error statusCode="404" redirect="/PageNotFound.aspx" />
</customErrors>
Easier way to test.
Try a bad url and watch your 404 error take over
Put this on your default.aspx page <% throw new Exception("gaah"); %>
And one other thing to consider: Error Logging. Can be achieved easily through ELMAH.

Problem with custom errors in ASP.NET

My setup:
Windows 7 Ultimate
IIS 7
Visual Studio 2008
The scenario:
building a simple website locally
an exception is occurring in my app (this is perfect, since I'm trying to setup a custom error page)
without customErrors setup in the web.config file, I get detailed info about the error
WITH customErrors setup in the web.config file, I get the following page when my error occurs:
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.
There's more text than that, but I'm sure you've all seen it before. At this point, the URL is:
http://localhost/bluheron/Error.aspx?aspxerrorpath=/bluheron
So, it looks like it redirected to my error page (Error.aspx), but I'm not seeing my error text ("An error has occurred. Plese try again."). Instead, I'm getting a funky URL with loads of other text, including instructions on how to setup my web.config file, which, by the way, contains this:
<customErrors mode="On" defaultRedirect="Error.aspx" />
It's in the default location, which is inside the system.web section, which is inside configuation section.
By the way, my Error.aspx page is in the root of my app.
Can someone explain what the fancy URL is all about and why my simple error page isn't displaying?
Thanks,
Jay
The error redirect is just what happens normally with customErrors...you can however disable this behavior and preserve the original url (not sending a 302 redirecting your user to the error page's url). This will execute/send the output of your error page:
Add redirectMode="ResponseRewrite" to your customErrors declaration:
<customErrors mode="On" defaultRedirect="Error.aspx" redirectMode="ResponseRewrite" />
If you're still getting the same behavior, there's something wrong with the whole application, and you should turn customErrors off and see what the issue is (this happens with an invalid web.config and many other cases).

Error Handling URLS that do not exist

I'm having problems with handling URLS that do not exist...
In my development environment, I navigated to http://localhost:XXXX/FakeLocation and would catch the HttpException and handle it properly.
When I deployed to my production location, I'm getting a 404 error when I navigate to http://MyProductionURL/FakeLocation. How can I make the production location throw the HttpException so my code can handle it similar to how it works in my development environment?
ASP.NET will only handle file extensions it is registered in IIS to handle. So if the page was foo.aspx, then by default, ASP.NET returns the 404 page as set in the web.config. And by default 404 for foo.xyz will be handled by IIS because IIS handles anything that doesn't have a mapping.
This page shows how to set up wildcard mapping so that all requests, regardless to extension are handled by asp.net.
You probably need to configure IIS on the production location. The easiest thing you can probably do is
Go to the Control Panel, and under Administrative Tools open Internet Information Services (IIS).
Right-click on your web site to bring up the properties.
Go to the Custom Errors tab
Select to the 404 error and edit properties on it.
Change the Message Type to URL.
Set the URL to the page you want them redirected to.
you can do this in the custom errors node of the web.config
<configuration>
<system.web>
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly">
<error statusCode="500"
redirect="InternalError.htm"/>
</customErrors>
</system.web>
</configuration>
Msdn Reference
Looks like IIS handle this exception and does not pass error to ASP.NET.
Please check http://www.chat11.com/How_To_Setup_A_Custom_404_Error_Handler_In_.NET

IIS 404 Custom Error not working as expected

I'm working on IIS6, ASP.NET, VS2008. The web site uses a custom error handler to catch 404 errors and serve an alternate page. When presented with an url of the form:
http://srv/crimson/articles/index
Everything works perfectly. But an url of the form:
http://srv/crimson/blog.aspx
Where blog.aspx does not exist, fails with the following message:
Server Error in '/Crimson' Application. Description: HTTP 404...
When I try and debug, none of the breakpoints in my 404 handler are hit. So it appears that something is catching the request earlier. Where? And how to I get it to pass the request on to my handler?
Edit
Thanks to those who answered, but none of those ideas worked. I've decided to attack the problem another way.
You might wanna try this:
In IIS6:
open "properties" for your website
go to "home directory" tab
click on "configuration"
look for the extension ".aspx"
click on "edit"
check the checkbox which says "verify that file exists"
edit
And what about this:
http://msdn.microsoft.com/en-us/library/h0hfz6fc(VS.80).aspx
<customErrors defaultRedirect="sorry.htm" mode="On">
<error statusCode="404" redirect="NotFound.aspx"/>
</customErrors>
Since 'RemoteOnly' specifies that custom errors are shown only to the remote clients, and that ASP.NET errors are shown to the local host.
hmm, Assaf is right, but to add to his answer I need to post some code.
Yes, Assaf does mean something else ASP.NET offers it's own error handling, configured through the web.config. You can either manage this through the IIS Admin snap in, or directly in the web.config file.
Within the <system.web> element you should have:
<customErrors defaultRedirect="sorry.htm" mode="RemoteOnly">
<error statusCode="404" redirect="NotFound.aspx"/>
</customErrors>
You can configure a different page for each HTTP error code, or let the default redirect handle them all.
You'll find that you do indeed need to set these error pages up in both the IIS custom errors and the ASP.NET configuration, as otherwise you'll end up in this situation - some pages go to your 404, and others use a default that you've not customised.
You should also make sure that your custom 404 page actually returns a 404 header to ensure that search engines etc treat it correctly.
Response.StatusCode = 404;
Response.StatusDescription = "Not found";
ASP.Net has a custom errors configuraiton of its own.
Go to the ASP.Net configuration of your virtual directory (web application) and right-click -> ASP.Net -> Edit Configuration -> Custom Errors.
Maybe you got a 500 so redirect for 404 is not triggered. You may focus on this 500 error at first.

Resources