Models to facilitate implementation and handling successful and error responses in web api.
Sample - Entity to ApiResponse
var apiResponse = myEntity.ToApiResponse(HttpStatusCode.OK);
Sample with Exception
// Creating a exception with errors
var errors = new ErrorsReponse();
response.AddError("message1", "property1");
response.AddError("message2", "property2");
throw new BadRequestException(errors);
// On your exception handler
var exceptionApiResponse = badRequestException.ToApiResponse();
You need implements
ApiResponse
mapping to your web api framework response, like ASP.NET or NancyFx model.
Sample - Casting ApiResponse to HttpResponseMessage
public class BaseApiController : ApiController
{
protected HttpResponseMessage CreateResponse(ApiResponse response)
{
var response = Request.CreateResponse(apiResponse.StatusCode, apiResponse.Content);
apiResponse.Headers.ForEach(header =>
{
response.Headers.Add(header.Key, header.Value);
});
return response;
}
}
public class MyController : BaseApiController
{
[HttpGet]
[Route("")]
public HttpResponseMessage GetSomething()
{
// do something and get ApiResponse
return this.CreateResponse(apiResponse);
}
}
PM> Install-Package WebApi.Models
Please, refer to CONTRIBUTING
Open a new Issue following our issue template ISSUE_TEMPLATE
See in nuget version history
if you liked this project, please make a contribution and help to keep this and other initiatives, send me some Satochis.
BTC Wallet: 1G535x1rYdMo9CNdTGK3eG6XJddBHdaqfX