Error404 and System.Web.Routing.RouteTable.Routes - asp.net

I'm using Routing in Asp.net 4.0/vb.net as example below:
rotas.MapPageRoute("test", "test/{detail}/{id}", "~/test/test.aspx")
In web.config has set the customErrors:
<customErrors defaultRedirect="ErrorPage.htm" mode="On">
<error statusCode="404" redirect="Error404.htm"/>
</customErrors>
A 404 error page appears correctly if an invalid url is submitted without using routing. Example: site/defaul2.aspx.
But if i enter an invalid url using routing like site/test2/anything/123 appears the standard asp.net 404 error page instead of my 404 custom page (Error404.htm).

If you are using IIS7, you may have to specify your custom error pages there as well.
Click on the site and select "Error Pages" (in IIS list) and change the entry for 404 and 500.
This wasn't a step I had to do in IIS6, but II6 did require a wildcard handler for URL Routing so ASP.NET handled requests for non-native files (i.e. jpg, png, pdf, etc.). IIS7 does this by default, but from my experience doesn't push to ASP.NET error pages for those assets - which is why you have to set those error pages explicitly.

Related

Asp.net Display 404 Screen for any 404 error

I have Asp.net website (Webforms). I want to Display custom page for 404 errors. I have following entry in web.config file:
<customErrors mode="On" defaultRedirect="~/error.aspx">
<error statusCode="404" redirect="~/404error.aspx"/>
</customErrors>
But this only redirects all pages with extensions. But Extension-less URLs like [http://website/abc/123] are not considered as page request, therefore there's no mapping for 404 custom page display. Instead I get browser generated 404 error page.
I have also tried to use Global.asax file's Application_Error method, but this error doesn't fall in this method for this case.
Please suggest to capture such 404 exceptions.
Note: I have read the following Link that also discussed the simillar situation, but I couldn't understand the resolution for this. I want this to happen even with Visual Studio Debugging.
Thanks
Actually I had to put code for displaying 404 error in my custom URLRewrite module. Where I handled all extension less URLs & finally at the end where url has been checked for all cases, I rendered 404 Error page, as that url was not meant for my website.
Thanks

How do you redirect a 404.3 message in ASP.NET?

In the case where a particular ActiveX control has not been installed on my user's workstation, attempting to load a particular file extension (e.g. "*.ica") causes a 404.3 status to be thrown. I would like to redirect this to an actual page instead of having the user see either the textual content of the file (lower versions of IE) or the 404.3 page (higher versions). I know how to do a custom errors redirect:
<customErrors mode="On">
<error statusCode="404" redirect="pageNotfound.aspx" />
</customErrors>
but I can't put 404.3 in the statusCode because it only takes integers, and there doesn't seem to be any other place to put the ".3". Besides what is shown above, the customErrors.errors node has only a few "lock" attributes available. Is there a way to catch a 404.3?
ETA: Since posting this question I have determined that IIS can be instructed to handle Custom Errors. I have tried this in IIS 7.5, but even setting the desired custom error page for 404.3 does not actually work.
It is still redirecting to the default 404.3 error page.

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