You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.4 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Filters;
using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace DS.WMS.WebApi;
/// <summary>
/// 基础服务
/// </summary>
public static class DsAppWebInstall
{
/// <summary>
/// 将模块服务添加到依赖注入服务容器中
/// </summary>
/// <param name="services">依赖注入服务容器</param>
/// <returns></returns>
public static IServiceCollection AddAppWebInstal(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
//注册http请求服务
services.AddHttpClient();
services.AddControllers(options =>
{
//全局异常过滤
options.Filters.Add<GlobalExceptionsFilter>();
options.SuppressAsyncSuffixInActionNames = false;
})
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressModelStateInvalidFilter = true;
//使用自定义模型验证
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<Object>.Failed(string.Join("|", errors));
return new JsonResult(result);
};
})
//全局配置Json序列化处理
.AddNewtonsoftJson(options =>
{
// 首字母小写(驼峰样式)
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
// 时间格式化
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
// 忽略循环引用
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
// 忽略空值
// options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
})
;
//添加FluentValidation验证
services.AddFluentValidationAutoValidation();
return services;
}
}