Working with Response Codes...
When developing custom IHTTP handlers; a developer has to handle when a parsed incoming url request does not match any dynamic content - or in other words - "404 Page Not Found". A well written IHTTP handler will most likely - when exhausting all possible matches, will usually redirect to a custom error page that maintains the sites branding.
However, depending on how the IHTTP handler is coded, it is quite possible that a HTTP Status code of 200 (success or OK) will be returned if the content requested is not found. For example, in my handler I consider a page not found as being that a particular content control could not be located, but the main page facade (similar to a Master Page) is found. So, I merely replace the placeholder with a control that displays a error message informing the user of the issue.
My approach was wrong; however, as again since the page is technically - successfully loaded - the wrong status code is sent to the client. I discovered this as I was testing a severe re-write of a custom redirect module - that actually tests the relative urls for a destination, to ensure the page is actually found. What I discovered was that I was able to input bogus relative urls and yet constantly my application returned a OK status. Not exactly what I wanted or expected. I certainly did not want to allow the inputs of a url re-mapping that would constantly 'redirect' to a error page.
One solution to the problem would be to just let the code execute through and have IIS return an actual 404 page. However, I really hate seeing 404 pages when browsing as one can not tell if its because a domain is just not configured or if it was just a missing page.
The second solution is to manually send a proper status code.
Very simple to do:
Response.StatusCode = (int)HttpStatusCode.NotFound;
It requires the using System.Net to function.
The HttpStatusCode is a very powerful feature for those working with streaming or IHTTP handlers. The ability to properly emit the correct response instead of just relying upon the Request.Response.Redirect to handle everything. For example you may need to do a redirect, but how to you inform a Google spider that it is a permanent redirection?
Here is code you might try:
if (Request.Browser.Crawler == true)
{
Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
}
//do response.redirect
This will allow a crawler to remove the url it used to hit your redirected content and replace it with the current one.
Another interesting status code is the HttpStatusCode.NoContent;
It will display a blank page in the browser when requested when it set to NoContent. That would be decent status code to send when someone is leeching links to images on your site or if you match malicious bot host names, IPs or User-Agents and do not want them to have access to the content.
If working with Ajax - beware that you properly code the setting of the status code properly.
This was actually interesting, I have my Ajax page that when submit is clicked - I perform the url validation in a web form, and spawn a separate HttpRequest from within code to check the url (see yesterdays blog post). I had set the Response.StatusCode = HttpStatusCode.NotFound; not knowing that I needed to convert it to an int. When the web request hit - I got a Sys.PageRequestManagerError immediately. This may be a threading issue or something however, I wouldn't have figured it would impact the web form that was loaded and working just fine (since it never gets to that code) as the check spawns a request outside the web form. Correcting the line of code to convert the status code enumeration to a int solved the problem and the Sys. error went away.