From HttpResponseMessage to IHttpActionResult
Coming from the first version of WebAPI
, we are all familiar with ApiController
actions that returns a HttpResponseMessage
:
public HttpResponseMessage Get(int id)
{
var doc = _documentations[id];
if (doc==null)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Documentation Not Found");
}
return Request.CreateResponse(doc);
}
Now, WebAPI provides us a better way to do the same job. Here is how the code above will be using IHttpActionResult
:
public IHttpActionResult Get(int id)
{
var doc = _documentations[id];
if (doc==null)
{
return NotFound();
}
return Ok(doc);
}
Easier to write, easier to read!
Written by Blaise Liu
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Mvc
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#