Asp.net Display 404 Screen for any 404 error - asp.net

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

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

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.

Error404 and System.Web.Routing.RouteTable.Routes

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.

I need a simple example of asp.net routing

if the user type
http://myweb/mysite.aspx (file does not exist)
I want them to go to
http://myweb/site.aspx (file does exist)
My goal is to make a bilingual website (including url) but without having to make physical file
this would be one file
http://myweb/acceuil.aspx
http://myweb/home.aspx
Not sure what you are trying to do, but this is the best turtorial for the question you asked:
How to: Use Routing with Web Forms
That's not routing; that is a redirect.
_rick_shott seems to have the mojo on routing bad urls into a 301 redirect. I upvoted his answer. You should check out his HTTPModule solution.
In your web.config, add a customErrors and error node as follows:
<customErrors mode="On" defaultRedirect="ErrorDisplayPage.aspx">
<error statusCode="404" redirect="http://myweb/site.aspx"/>
</customErrors>
This will displayErrorDisplayPage.aspx for all unmanaged errors except for 404 errors (which are "page not found"). For 404 errors, the browser will redirect to the site.aspx page.

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