|
|
@ -13,16 +13,19 @@ public static class ControllerSetup
|
|
|
|
/// 配置controller,包含过滤器、json序列化以及模型验证等配置
|
|
|
|
/// 配置controller,包含过滤器、json序列化以及模型验证等配置
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="services"></param>
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
|
|
|
/// <param name="configureControllers"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static IServiceCollection AddDefaultControllers(this IServiceCollection services)
|
|
|
|
public static IServiceCollection AddDefaultControllers(this IServiceCollection services,
|
|
|
|
|
|
|
|
Action<IMvcBuilder>? configureControllers = null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
|
|
services.AddControllers(options =>
|
|
|
|
var mvcBuilder = services.AddControllers(options =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
options.Filters.Add<ExceptionsFilter>();
|
|
|
|
options.Filters.Add<ExceptionsFilter>();
|
|
|
|
options.Filters.Add<IdempotencyFilter>();
|
|
|
|
options.Filters.Add<IdempotencyFilter>();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
.AddNewtonsoftJson(options =>
|
|
|
|
|
|
|
|
|
|
|
|
mvcBuilder.AddNewtonsoftJson(options =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
|
|
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
|
|
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
|
|
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
|
@ -30,8 +33,8 @@ public static class ControllerSetup
|
|
|
|
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
|
|
|
|
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
|
|
|
|
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
|
|
|
|
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
|
|
|
|
options.SerializerSettings.Converters.Add(new StringEnumConverter());
|
|
|
|
options.SerializerSettings.Converters.Add(new StringEnumConverter());
|
|
|
|
})
|
|
|
|
});
|
|
|
|
.ConfigureApiBehaviorOptions(options =>
|
|
|
|
mvcBuilder.ConfigureApiBehaviorOptions(options =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
options.InvalidModelStateResponseFactory = _ =>
|
|
|
|
options.InvalidModelStateResponseFactory = _ =>
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -39,6 +42,7 @@ public static class ControllerSetup
|
|
|
|
return new OkObjectResult(message.Serialize());
|
|
|
|
return new OkObjectResult(message.Serialize());
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
configureControllers?.Invoke(mvcBuilder);
|
|
|
|
return services;
|
|
|
|
return services;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|