Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
DZone MVB

Links

Social

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).

Example

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
                    select p)
                    .FirstOrDefault();

    // if product is null then it has never been there - say it's not found
    if (product == null)
        return HttpNotFound();

    // 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
    return View(product);
}

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.

Conclusion

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.

Posted: Jul 28 2010, 11:42 AM by DigiMortal | with 10 comment(s)
Filed under: , ,

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# July 28, 2010 4:50 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# July 28, 2010 4:51 AM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# July 28, 2010 4:53 AM

Twitter Trackbacks for ASP.NET MVC 3: Using HttpStatusCodeResult - Gunnar Peipman's ASP.NET blog [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 ASP.NET MVC 3: Using HttpStatusCodeResult - Gunnar Peipman's ASP.NET blog         [asp.net]        on Topsy.com

# July 28, 2010 5:27 AM

Gunnar Peipman's ASP.NET blog said:

My last posts described new action results in ASP.NET MVC 3 – HttpNotFoundResult and HttpStatusCodeResult

# July 28, 2010 7:11 AM

ASP.NET MVC 3: Using HttpStatusCodeResult – Gunnar Peipman's ASP … - asp said:

Pingback from  ASP.NET MVC 3: Using HttpStatusCodeResult – Gunnar Peipman's ASP … - asp

# July 28, 2010 9:42 AM

Webprogrammierung & Design » Blog Archive » Murach’s ASP.NET 2.0 Web Programming with VB 2005: training & reference said:

Pingback from  Webprogrammierung & Design  » Blog Archive   » Murach’s ASP.NET 2.0 Web Programming with VB 2005: training & reference

# July 28, 2010 8:46 PM

Gunnar Peipman's ASP.NET blog said:

Lately I blogged about HttpNotFoundResult and HttpStatusCodeResult action results that come with ASP

# August 15, 2010 12:46 PM

Ant said:

Can you explain how we can show a custom page for this type of thing?

thanks for the post btw :)

# January 29, 2011 8:59 AM

DigiMortal said:

# January 31, 2011 3:57 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)