This project has been moved in the ForEvolve-Framework repository.
A dynamic internal server error filter for ASP.NET Core, targetting NetStandard 1.6 and Asp.Net Core 2.0, that translate Exceptions (status code 500) to JSON automatically.
Validation errors are also translated automatically, following the same convention, for response with status code 400.
The error model is based on: Microsoft REST API Guidelines.
This is a work in progress.
ForEvolve NuGet V3 feed URL packages source. See the Table of content project for more info.
In your Startup
class, you must AddDynamicInternalServerError()
to add dependencies and ConfigureDynamicInternalServerError()
to add the filter to MVC.
public void ConfigureServices(IServiceCollection services)
{
// Add DynamicInternalServerError
services.AddDynamicInternalServerError();
// Add framework services.
services.AddMvc(options =>
{
options.ConfigureDynamicInternalServerError();
});
}
Thats it, now exceptions are translated to JSON.
In the ConfigureServices
method, you must add services.AddDynamicInternalServerErrorSwagger();
.
In the services.AddSwaggerGen(c => { ... })
you must register swagger operation filters by calling c.AddDynamicInternalServerError();
.
public void ConfigureServices(IServiceCollection services)
{
// Add DynamicInternalServerError
services.AddDynamicInternalServerError();
services.AddDynamicInternalServerErrorSwagger();
// Add framework services.
services.AddMvc(options =>
{
options.ConfigureDynamicInternalServerError();
});
// Add and configure Swagger.
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
// Add and configure DynamicInternalServerError.
c.AddDynamicInternalServerError();
});
}
DynamicValidationActionFilter
translates BadRequestObjectResult
that has a value of type SerializableError
to an ErrorResponse
object, following the same convention as DynamicValidationActionFilter
do.
By default, by registering DynamicInternalServerError
you also register DynamicValidation
.
Return BadRequest(ModelState);
in a controller action. To define ErrorResponse
automatically in the swagger definition file, simply decorat the action with [ProducesResponseType(400)]
(do not specify a type).
[HttpPost]
[ProducesResponseType(400)]
public IActionResult Post([FromBody]string value)
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// ...
}