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.

53 lines
1.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DS.Module.Core.Extensions;
using Microsoft.AspNetCore.Builder;
using Swashbuckle.AspNetCore.SwaggerUI;
namespace DS.Module.Core.Middlewares
{
/// <summary>
/// 封装通用中间件
/// </summary>
public static class UseCommonMiddlewares
{
///<summary>
///为了让主Progarm.cs文件更加简洁我们将一些中间件的配置放到这里
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static WebApplication UsePublicMiddlewares(this WebApplication app)
{
var documentName = AppSetting.app(new string[] { "SwaggerDoc", "ContactName" });
app
.UseSwagger(c => { c.RouteTemplate = "{documentName}/swagger.json"; })
.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/" + documentName + "/swagger.json",
AppSetting.app(new string[] { "SwaggerDoc", "ContactName" }));
c.DocExpansion(DocExpansion.None);//DocExpansion设置为None可折叠所有方法
c.DefaultModelExpandDepth(-1);//-1 可不显示Models
});
//跨域
var policyName = AppSetting.app(new string[] { "Cors", "PolicyName" });
if (!policyName.IsNullOrEmpty())
{
app.UseCors(policyName); //添加跨域中间件
}
app.UseRouting();
app.UseStaticFiles();
// //操作日志中间件
// app.UseMiddleware<OperationLogMiddleware>();
// 先开启认证
app.UseAuthentication();
// 然后是授权中间件
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
return app;
}
}
}