28 07 2010
ASP.NET MVC 3: Using HttpStatusCodeResult
ASP.NET MVC 3 also introduces new action result HttpStatusCodeResult. This action result provides you way how to specify HTTP status code and status description in your controllers. In this posting I will show you how to use HttpStatusCodeResult in your real-world applications providing meaningful status code for products that were online once but are now gone and we don’t expect them to be back in future.
What is HTTP status code 410 – Gone?
In this example I will use HTTP code that you don’t hear often about. It is 410 – Gone. You may argue that stauts code 404 – Not Found is enough but there is one little gotcha – 410 is considered as permanent response. Cite from RFC 2616 Hypertext Transfer Protocol – HTTP/1.1 chapter 10. Status Code Definitions:
“The requested resource is no longer available at the server and no forwarding address is known. This condition is expected to be considered permanent. Clients with link editing capabilities SHOULD delete references to the Request-URI after user approval. If the server does not know, or has no facility to determine, whether or not the condition is permanent, the status code 404 (Not Found) SHOULD be used instead. This response is cacheable unless indicated otherwise.
The 410 response is primarily intended to assist the task of web maintenance by notifying the recipient that the resource is intentionally unavailable and that the server owners desire that remote links to that resource be removed. Such an event is common for limited-time, promotional services and for resources belonging to individuals no longer working at the server’s site. It is not necessary to mark all permanently unavailable resources as "gone" or to keep the mark for any length of time — that is left to the discretion of the server owner.”
In online e-commerce systems, by example, we have all products we have sold. So we can determine easily if there is product that was available once but it is sure it never comes back to our store (okay, wise men say: never say never).
Let’s suppose we have online e-commerce system and we have detail pages for products. These pages are dynamic and we can check from database if product is available. We have some products we don’t plan to sell anymore and we can determine if request was made for some of those products.
This is fictional code and I added here some comments too so you have better understanding what is going on.
public ActionResult Details(int productId)
// ask products from context that was injected by controller factory
var product = (from p in _context.Products
where p.Id == productId
// if product is null then it has never been there - say it's not found
if (product == null)
// product is out of sales and it never comes back - say it's gone
if (product.SalesFinished == null)
return new HttpStatusCodeResult(410);
// product found - show details
Now, when visitor asks product that was there once and for now it is gone forever his or her browser gets information from server that all links to this resource must be removed. This message also tells to search engine robots that given link must be removed from index and all pages having links to this page must be handled as pages with broken links.
Using HttpStatusCodeResult you can improve SEO of your pages and make your site more understandable to robots and other clients implemented in software. Status code 410 – Gone expects that you have some place where you can check if asked resource has been alive before or not. If your system doesn’t contain this data then you should use status code 404 – Not Found instead of 410 – Gone.