/// <summary> /// Mvc 注入基础配置 /// </summary> /// <param name="mvcBuilder">Mvc构建器</param> /// <param name="includeDynamicApiController"></param> /// <returns>IMvcBuilder</returns> public static IMvcBuilder AddInjectBase(this IMvcBuilder mvcBuilder, bool includeDynamicApiController = true) { if (includeDynamicApiController) { mvcBuilder.AddDynamicApiControllers(); } mvcBuilder.AddDataValidation() .AddFriendlyException(); return(mvcBuilder); }
/// <summary> /// 添加全局数据验证 /// </summary> /// <typeparam name="TValidationMessageTypeProvider">验证类型消息提供器</typeparam> /// <param name="mvcBuilder"></param> /// <param name="enabledGlobalDataValidationFilter">启用全局验证过滤器</param> /// <returns></returns> public static IMvcBuilder AddDataValidation <TValidationMessageTypeProvider>(this IMvcBuilder mvcBuilder, bool enabledGlobalDataValidationFilter = true) where TValidationMessageTypeProvider : class, IValidationMessageTypeProvider { var services = mvcBuilder.Services; // 添加全局数据验证 mvcBuilder.AddDataValidation(enabledGlobalDataValidationFilter); // 单例注册验证消息提供器 services.TryAddSingleton <IValidationMessageTypeProvider, TValidationMessageTypeProvider>(); return(mvcBuilder); }
/// <summary> /// 添加全局数据验证 /// </summary> /// <typeparam name="TValidationMessageTypeProvider">验证类型消息提供器</typeparam> /// <param name="mvcBuilder"></param> /// <param name="enabledGlobalDataValidationFilter">启用全局验证过滤器</param> /// <param name="suppressImplicitRequiredAttributeForNonNullableReferenceTypes">禁止C# 8.0 验证非可空引用类型</param> /// <returns></returns> public static IMvcBuilder AddDataValidation <TValidationMessageTypeProvider>(this IMvcBuilder mvcBuilder, bool enabledGlobalDataValidationFilter = true, bool suppressImplicitRequiredAttributeForNonNullableReferenceTypes = true) where TValidationMessageTypeProvider : class, IValidationMessageTypeProvider { var services = mvcBuilder.Services; // 添加全局数据验证 mvcBuilder.AddDataValidation(enabledGlobalDataValidationFilter, suppressImplicitRequiredAttributeForNonNullableReferenceTypes); // 单例注册验证消息提供器 services.AddSingleton <IValidationMessageTypeProvider, TValidationMessageTypeProvider>(); return(mvcBuilder); }