https://github.com/thiagobarradas/webapi-models
Models to facilitate implementation and handling successful and error responses in web api.
https://github.com/thiagobarradas/webapi-models
api api-response dotnet dotnet-core dotnet-standard exception generic models response webapi
Last synced: about 1 year ago
JSON representation
Models to facilitate implementation and handling successful and error responses in web api.
- Host: GitHub
- URL: https://github.com/thiagobarradas/webapi-models
- Owner: ThiagoBarradas
- License: mit
- Created: 2018-09-19T05:25:46.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-28T20:03:47.000Z (almost 2 years ago)
- Last Synced: 2025-04-08T23:36:36.344Z (about 1 year ago)
- Topics: api, api-response, dotnet, dotnet-core, dotnet-standard, exception, generic, models, response, webapi
- Language: C#
- Homepage: https://www.nuget.org/packages/WebApi.Models/
- Size: 38.1 KB
- Stars: 5
- Watchers: 0
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://barradas.visualstudio.com/Contributions/_build/latest?definitionId=18&branchName=develop)
[](https://sonarcloud.io/dashboard?id=ThiagoBarradas_webapi-models)
[](https://sonarcloud.io/dashboard?id=ThiagoBarradas_webapi-models)
[](https://www.nuget.org/packages/WebApi.Models/)
[](https://www.nuget.org/packages/WebApi.Models/)
# WebApi.Models
Models to facilitate implementation and handling successful and error responses in web api.
# Sample
Sample - Entity to ApiResponse
```c#
var apiResponse = myEntity.ToApiResponse(HttpStatusCode.OK);
```
Sample with Exception
```c#
// 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
```c#
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);
}
}
```
## Install via NuGet
```
PM> Install-Package WebApi.Models
```
## How can I contribute?
Please, refer to [CONTRIBUTING](.github/CONTRIBUTING.md)
## Found something strange or need a new feature?
Open a new Issue following our issue template [ISSUE_TEMPLATE](.github/ISSUE_TEMPLATE.md)
## Changelog
See in [nuget version history](https://www.nuget.org/packages/WebApi.Models)
## Did you like it? Please, make a donate :)
if you liked this project, please make a contribution and help to keep this and other initiatives, send me some Satochis.
BTC Wallet: `1G535x1rYdMo9CNdTGK3eG6XJddBHdaqfX`
