ASP.NET MVC 3: Creating HttpStatusCodeResult with view based body

My last posts described new action results in ASP.NET MVC 3 – HttpNotFoundResult and HttpStatusCodeResult. Unfortunately these result have no body to send to client but there are times we need it to inform our visitor about problems the way that we don’t go out of information context where visitor is. In this example I will show you how to write your own HttpStatusCodeResult that has also body.

We cannot use HttpStatusCodeResult because it doesn’t contain information about current view and there is no way how to attach views to this action result. Instead, I wrote my own implementation of HttpStatusCodeResult that extends ViewResult class and has therefore support for views. This is important thing because body of response is defined same way as other views in ASP.NET MVC framework.

Okay, here is my HttpStatusCodeWithBodyResult class in its very first form.


public class HttpStatusCodeWithBodyResult : ViewResult
{
   
private int
_statusCode;
   
private string
_description;

   
public HttpStatusCodeWithBodyResult(int
statusCode,
           
string description = null
)
        :
this(null
, statusCode, description)
    {

    }
   
public HttpStatusCodeWithBodyResult(string viewName, int
statusCode,
           
string description = null
)
    {
        _statusCode = statusCode;
        _description = description;
        ViewName = viewName;
    }

   
public override void ExecuteResult(ControllerContext
context)
    {
       
var
httpContext = context.HttpContext;
       
var
response = httpContext.Response;

        response.StatusCode = _statusCode;
        response.StatusDescription = _description;

       
base.ExecuteResult(context);
    }
}

And here is how you can use it in you controller methods. The examples are given for 404 – Not Found and 410 – Gone responses.


public ActionResult NotFound()
{
   
var msg = "I cannot find this resource!"
;

   
// there must be view called NotFound.aspx or NotFound.cshtml
    var result = new HttpStatusCodeResultWithBody("NotFound"
, 404, msg);
   
return
result;
}

public ActionResult
GoneForever()
{
   
var msg = "This resource is dead and buried!"
;

   
// there must be view called GoneForever.aspx or GoneForever.cshtml
    var result = new HttpStatusCodeResultWithBody
(410, msg);
   
return result;
}

You can read more about HttpNotFoundResult class from my posting ASP.NET MVC 3: Using HttpNotFoundResult action result and about HttpStatusCodeResult from my posting ASP.NET MVC 3: Using HttpStatusCodeResult.

Conclusion

In this posting I showed you how to create action result that works like HttpSthatusCodeResult but supports also body for result returned to browser. This way we can tell to both technical clients (browsers, search engine spiders etc) and visitors that there was problem and what one or another of them has to do now. Consider it also as good SEO and usability practices.


Leave a Reply

Your email address will not be published. Required fields are marked *