using DS.Module.Core;
using DS.Module.Core.Filters;
using DS.Module.Core.Helpers;
using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
namespace DS.Module.Core.ServiceExtensions;
///
/// 基础服务
///
public static class DsAppWebInstall
{
///
/// 将模块服务添加到依赖注入服务容器中
///
/// 依赖注入服务容器
///
public static IServiceCollection AddAppWebInstal(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
//注册http请求服务
services.AddHttpClient();
services.AddControllers(options =>
{
options.Filters.Add();
// 204预检查过滤
//options.Filters.Add();
//全局异常过滤
options.Filters.Add();
////全局操作日志
//options.Filters.Add();
options.SuppressAsyncSuffixInActionNames = false;
//关闭不可为空引用类型的属性
options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true;
})
.ConfigureApiBehaviorOptions(options =>
{
//SuppressModelStateInvalidFilter =true时,会关闭默认模型验证过滤器。[ApiController] 默认自带有400模型验证,且优先级比较高,如果需要自定义模型验证,则需要先关闭默认的模型验证。
options.SuppressModelStateInvalidFilter = false;
//使用自定义模型验证
options.InvalidModelStateResponseFactory = (context) =>
{
var errors = context.ModelState
.Where(e => e.Value.Errors.Count > 0)
.Select(e => e.Value.Errors.First().ErrorMessage)
.ToList();
var result = DataResult