Files
kunshan-bzfm-mes-backend/DOAN.Common/DynamicApiSimple/Extens/DynamicApiExtens.cs
qianhao.xu 586d2e60dc init
2024-12-03 09:25:16 +08:00

53 lines
1.8 KiB
C#

using Infrastructure.Helper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Linq;
using System.Reflection;
namespace DOAN.Common.DynamicApiSimple.Extens
{
public static class DynamicApiExtens
{
public static string TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
/// <summary>
/// 注入动态api
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddDynamicApi(this IServiceCollection services)
{
services.AddMvc()
.ConfigureApplicationPartManager(m =>
{
foreach (Assembly assembly in AssemblyUtils.GetAssemblies())
{
if (m.ApplicationParts.Any(it => it.Name.Equals(assembly.FullName.Split(',')[0]))) continue;
m.ApplicationParts.Add(new AssemblyPart(assembly));
}
m.FeatureProviders.Add(new ApiFeatureProvider());
}).AddNewtonsoftJson(options =>
{
options.SerializerSettings.DateFormatString = TIME_FORMAT_FULL;
options.SerializerSettings.Converters.Add(new IsoDateTimeConverter
{
DateTimeFormat = TIME_FORMAT_FULL,
});
// 设置为驼峰命名
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
services.Configure<MvcOptions>(o =>
{
o.Conventions.Add(new ApiConvention());
});
return services;
}
}
}