parent
be9552b1f0
commit
c801e599ea
@ -0,0 +1,40 @@
|
||||
using Infrastructure.Filters;
|
||||
using Infrastructure.Utils;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Infrastructure.Extensions;
|
||||
|
||||
public static class ControllerSetup
|
||||
{
|
||||
public static IServiceCollection AddDefaultControllers(this IServiceCollection services)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(services);
|
||||
services.AddControllers(options =>
|
||||
{
|
||||
options.Filters.Add<ExceptionsFilter>();
|
||||
options.Filters.Add<IdempotencyFilter>();
|
||||
})
|
||||
.AddNewtonsoftJson(options =>
|
||||
{
|
||||
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||||
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
||||
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
||||
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
|
||||
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
|
||||
options.SerializerSettings.Converters.Add(new StringEnumConverter());
|
||||
})
|
||||
.ConfigureApiBehaviorOptions(options =>
|
||||
{
|
||||
options.InvalidModelStateResponseFactory = context =>
|
||||
{
|
||||
var message = new MessageData(false, "the input wat not valid", 400);
|
||||
return new OkObjectResult(message.Serialize());
|
||||
};
|
||||
});
|
||||
return services;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue