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


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


This article is related to

C#,.NET,Architect,Intermediate,VS2010,.Net,Articles,Computer Tutorials, ASP.Net