IIS 7.5 Logs and ASP.Net 404 Custom Pages - asp.net

I want to make a custom 404 page for my site but I want to log all the normal information into my IIS 7.5 logs like the default 404 deals with so my Statics program can tell me things like what page got the 404 error, what was the referring URL to that broken page, and more. Do I have to do anything special on my 404 page to do this or has ASP evolved enough to automatically do logging for me if I return a 404 status code?

How you handle the errors determines how the errors will show up in the logs/responses. If you simply use custom error pages, it will show up no different in the log than if you had used the OOTB pages. If, however, you are writing an ASP.NET application, and handle/bury the exception, nothing will show up in the logs.
If you are writing a .NET application, this blog post provides a pretty good overview on how to properly handle errors for SEO.

ASP.NET Custom Error pages can be implemented in many different ways. The "worst" way are those that return a 301 redirection to "NotFound.aspx" (or similar), which will, of course, return a 200 status code. Unfortunately the IIS Manager actually lets you specify this method. If you're finding your 404s and 500s aren't being logged then check out this setting first.
Error pages, regardless of their implementation, must not issue any redirection and must set the status code to 404, that way IIS's logger will log it accordingly (IIS inspects the headers of all outgoing responses and uses that to populate the log).

Related

IIS - Unexplained 302 response code

What scenarios cause IIS to generate a 302 besides response.redirect? I'm working on an legacy ASP.NET app and it's generating 302s in IIS. The thing is, the code doesn't make any response.redirect calls. I don't know how to debug this thing. Could losing session generate a 302? I'm totally lost.
It can happen if you return a CSS file in ASP.NET over http when it should be over https, ive seen that happen in the past. Also if any web services tied into that application that are getting hit or making passes that fail, that could cause it too. Just shooting from my hip and trying to recall the last time I seen those issue's arise.
Check at the IIS level, not ASP.NET level.
Check the IIS management console. Possible options are:
"URL rewrite module"
"HTTP redirect" (under the website settings, for IIS7)

Handling 404 error in ASP.NET Web Forms with URL Routing

I'm looking at implementing URL Routing on an ASP.NET Web Forms website and have been looking at the best way to handle 404 errors so for example:
routes.MapPageRoute("NewsRouteArticle", "news/article/{url}", "~/news.aspx")
The URL is matched against a news article in the database.
What happens if a news article no longer exists? How can a handle this and redirect the user to the Custom Error page?
I have been reading about Throw New HttpResponseException(HttpStatusCode.NotFound) but it looks like it is for ASP.NET Web API.
I'm also aware of the issue that exists with ASP.NET where 404 Errors actually gives a status code of 302 and I want to try and avoid the issue (see this).
One good reason to use ASP.NET.MVC, I have not done anything in webforms for so long, but I used
Server.TransferRequest("/my404page.aspx")
This stopped the 2 requests and the 302 to 404 problem
then returned a 404
Response.Clear()
Response.TrySkipIisCustomErrors = True
Response.StatusCode = 404
Hope that helps

Custom http error pages for images?

I know it's common for pages to redirect to a custom error page for 404 errors and such. But say somebody opens the url in their browser for an image that doesn't exist for example http://mysite.com/nothinghere.gif. This site returns a 404 error but it's not just the browser default, it's a custom 404. How do you configure an ASP.NET site on IIS7 to do this? the customerrors section in the web.config doesn't seem to apply to things like images, css, js, etc
customErrors section would apply to resources managed by ASP.NET. IIS7 has its own httpErrors section under webserver node. Of course, there is also UI to configure it. Anyway error handling behavior also changes based on you are running your ASP.NET application under classic mode or integrated mode. I will suggest that you read following articles to get hold of it.
http://www.braintrove.com/article/46 - this will tell you how to set up IIS7's own cutom error pages
Below explains IIS7 errors handling
http://learn.iis.net/page.aspx/267/how-to-use-http-detailed-errors-in-iis-70/
http://blogs.iis.net/ksingla/archive/2008/02/18/what-to-expect-from-iis7-custom-error-module.aspx
Make your custom error page a script.
When its called you can either send a html response for a page missing or query the request to find out the image name and then send an image or redirect to an image.
try these links for issues with custom 404 errors
IIS 404 Custom Error not working as expected
http://forums.asp.net/p/1603843/4089618.aspx

ASP.NET/IIS: 404 for all file types

I set up 404 handler page in web.config, but it works ONLY when extension of URL is .aspx (or other which is handled by ASP.NET).
I know I can setup static HTML page in website options, but I want to have a page.
Is there any options to assign ASPX handler page for all request extensions in IIS?
The direct question was whether or not there are options to assign the ASPX handler to all request extensions: Yes, there is. I'll discuss how to do that shortly.
First, I think the "hidden" question -- the answer you really want -- is whether or not there's a way to redirect all 404 errors for pages other than ASPX, ASMX, etc. Yes, there is, and this is the better choice if it'll solve the issue you're having.
To redirect all 404s in IIS 6, right click your web application root (whether it be its own site or a virtual directory in the main site), and choose "Properties." From there, choose the "Custom Errors" tab. Find 404 in the list and change it to the redirect you want.
Now, if that won't suffice -- and I really hope it does -- yes, you can run every page through the ASPX handler. However, doing so comes at a fairly high cost in terms of efficiency -- raw HTML/image serving is considerably faster than anything dynamic.
To do this, right click your web application root and choose "Properties." Choose the "Home Directory" tab. Click "Configuration;" a new window will pop up. Copy the path from one of the ASP.NET page serves, and then use it for a wildcard application map.
Bear in mind, again, this is the wrong answer most of the time. It will negatively impact your performance, and is the equivalent of using a chainsaw to carve a turkey. I highly recommend the first option over this one, if it will work out for you.
For information:
This is one of the several nice things that IIS7 brings - all pages are routed through the handler such that you can do custom 404s and - usefully - directory and file level security for any file (based on the same web.config stuff as for asp.net files prior to IIS7).
So notionally "use II7" is an answer (will be "the" answer in time) - but of course its not a terribly practical one if you're not hosting/being hosted on W2k8 (or higher).
The web.config can only set up errors pages for pages controlled by it's web site. If you have any other pages outside the purview of the ASP.Net application, then you set up handling for them in IIS. There's an option in there for configuring the 404 page where you can point it to your custom page.
Only other thing i can think of is passing ALL extensions to asp.net.
This way all types of files get processed by asp.net and your custom error page will work.
In the IIS application configuration, you can set a wildcard mapping (".*") to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
You can setup wild card mapping in IIS (Application configuration/Mappings/Wildcard mappings/ - just set aspnet_isapi.dll as executable and uncheck the Verify that file exists box) that will route all incoming requests to your app - so you can control the behavior directly from it.
You don't have to setup static page in your IIS application settings. Imho, you should be able to setup valid url (e.g. /error_handler.aspx) from your app that will be used as landing page in case of specific server error.
In IIS you can set a Custom Error for 404 errors and direct it to a URL in the site properties.
It shows a static html by default
C:\WINDOWS\help\iisHelp\common\404b.htm
You can change it to a relative url on your site.

Custom URL Extensions/Routing Without IIS Access

I have a need to use extensionless URLs. I do not have access to IIS (6.0) so I cannot map requests to ASP.NET and handle with a HttpHandler/HttpModule. However, I can set a custom 404 page via web host control panel.
My current plan is to perform necessary logic in the custom 404 page, but it "feels wrong". Are there any recommendations that I am missing?
Edited: Added "Without IIS Access" to the title since someone thought this was a repeat question.
Without access to IIS, that would be your only option.
The 404 page really is your only option if you can't map the requests. I've seen several blog packages that do this to enable magic URLs like .../archive/YYYY/MM/DD and such - there's no such page, so it hits the 404 page and the 404 page does the redirection.

Resources