Exception Handling in ASP.NET Web API

Advertisements

  Top Tutorials     New Tutorials     Submit     Login     Register  

Tutorial Detail

Exception Handling in ASP.NET Web API

We can simply return respective error status code from Web API service instead of a 500 generic error code i.e. Internal Server Error.
http://www.webdevelopmenthelp.net/2014/01/Exception-Handling-ASP.NET-Web-API.html
  • Total Hits: 23419

  • Average Rating :
    URL RatingURL RatingURL RatingURL RatingURL Rating

  •  

  • Total Votes: 230 votes

  • Category: ASP.Net/ASP.Net Misc

  • Submitted on: 2014-01-27 01:06:37

  • Submitted By: Imran Ghani

  • Set as Favorite

Description:

In one of my previous articles, we explored how to handle Exceptions in WCF RESTful services. In this article the focus is same to handle exceptions but for ASP.NET Web API service. HttpResponseException class plays its role to return HTTP status code as well as further exception details to client.

We can simply return respective error status code from Web API service instead of a 500 generic error code i.e. Internal Server Error. Here we will implement exception handling for the same ASP.NET Web API service created earlier in a tutorial i.e. "Simply create ASP.NET Web API service with all CRUD operations".

Now, we have Web API service controller StudentController having GET method taking "Id" as parameter and returns a student against it. If student doesn't exists against a provided student Id, then we can handle and throw the exception with respective status code i.e. "Not Found".

After adding the exception handling code using HttpResponseException class, GET method implementation will be as follows:
public Student Get(string id)
{
Student student = StudentRepository.GetStudent(id);
if (student == null)
throw new HttpResponseException(HttpStatusCode.NotFound);

return student;
}

It looks nice to return logical status code back to client but what if we wanted to add more error details?
ASP.NET Web API allows us to create response message using HttpResponseMessage class and pass it to HttpResponseException.

HttpResponseMessage represents a returning response message having following important properties:

StatusCode is HTTP response status code i.e. NotFound in our case.
ResonPhrase to return the exception reason along with status code.
Content property can be used to set or get HTTP response content.

public Student Get(string id)
{
Student student = StudentRepository.GetStudent(id);
if (student == null)
{
HttpResponseMessage responseMessage = new HttpResponseMessage();
responseMessage.StatusCode = HttpStatusCode.NotFound;
responseMessage.ReasonPhrase = "Student not found";
responseMessage.Content = new StringContent("No student exists against provided student id");

throw new HttpResponseException(responseMessage);
}
return student;
}

Note: Remember one important thing about HttpResponseMessage class. Initially, we can easily create a type and pass it to HttpResponseMessage class constructor and return that message with data. But now we can only use Content property to set message content.

Hopefully, this web application development tutorial will be helpful in order to understand and implement exception handling in ASP.NET Web API services.    More detail...

Rating Detail

 Stars  Percentage  Total Vote
 One star  Vote LeftVote MiddleVote Right 50.87%  117
 Two starsTwo star  Vote LeftVote MiddleVote Right 8.7%  20
 Three starsThree starsThree stars  Vote LeftVote MiddleVote Right 8.26%  19
 Four starsFour starsFour starsFour stars  Vote LeftVote MiddleVote Right 8.26%  19
 Five starsFive starsFive starsFive starsFive stars  Vote LeftVote MiddleVote Right 23.91%  55

Caste Your Vote

 Rating  Selection  Guidelines
 Poor:     Very disappointing, useless.
 Fair:     Below average, disappointing but not useless.
 Good:     About average in its class.
 Very Good:     Above average for its category.
 Excellent:     One of the best Tutorials in its category.





Advertisements
Advertisements