|
|
|
@ -13,16 +13,23 @@ public static class ControllerSetup
|
|
|
|
|
/// 配置controller,包含过滤器、json序列化以及模型验证等配置
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
/// <param name="configureControllers"></param>
|
|
|
|
|
/// <param name="configureControllers">配置Controllers</param>
|
|
|
|
|
/// <param name="configureMvcOptions">mvc配置选项</param>
|
|
|
|
|
/// <param name="configureMvcNewtonsoftJsonOptions">json序列化配置选项</param>
|
|
|
|
|
/// <param name="configureApiBehaviorOptions">api行为配置选项</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IServiceCollection AddDefaultControllers(this IServiceCollection services,
|
|
|
|
|
Action<IMvcBuilder>? configureControllers = null)
|
|
|
|
|
Action<IMvcBuilder>? configureControllers = null,
|
|
|
|
|
Action<MvcOptions>? configureMvcOptions = null,
|
|
|
|
|
Action<MvcNewtonsoftJsonOptions>? configureMvcNewtonsoftJsonOptions = null,
|
|
|
|
|
Action<ApiBehaviorOptions>? configureApiBehaviorOptions = null)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
|
|
|
var mvcBuilder = services.AddControllers(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Filters.Add<ExceptionsFilter>();
|
|
|
|
|
options.Filters.Add<IdempotencyFilter>();
|
|
|
|
|
configureMvcOptions?.Invoke(options);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mvcBuilder.AddNewtonsoftJson(options =>
|
|
|
|
@ -33,6 +40,7 @@ public static class ControllerSetup
|
|
|
|
|
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
|
|
|
|
|
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
|
|
|
|
|
options.SerializerSettings.Converters.Add(new StringEnumConverter());
|
|
|
|
|
configureMvcNewtonsoftJsonOptions?.Invoke(options);
|
|
|
|
|
});
|
|
|
|
|
mvcBuilder.ConfigureApiBehaviorOptions(options =>
|
|
|
|
|
{
|
|
|
|
@ -41,6 +49,7 @@ public static class ControllerSetup
|
|
|
|
|
var message = new MessageData(false, "the input value not valid", 400);
|
|
|
|
|
return new OkObjectResult(message.Serialize());
|
|
|
|
|
};
|
|
|
|
|
configureApiBehaviorOptions?.Invoke(options);
|
|
|
|
|
});
|
|
|
|
|
configureControllers?.Invoke(mvcBuilder);
|
|
|
|
|
return services;
|
|
|
|
|