Monday, 31 December 2018

How to handle errors using HttpError?

CreateErrorResponse is an extension method defined in the System.Net.Http.HttpRequestMessageExtensions class. Internally, CreateErrorResponse creates an HttpError instance and then creates an HttpResponseMessage that contains the HttpErrorThe HttpError object provides a consistent way to return error information in the response body.

public HttpResponseMessage GetEmployee(int Empid) { Employee emp = repository.Get(Empid); if (emp == null) { var message = string.Format("Employee with Empid = {0} not found", Empid); return Request.CreateErrorResponse(HttpStatusCode.NotFound, message); } else { return Request.CreateResponse(HttpStatusCode.OK, emp); } }

No comments:

Post a Comment