1 、在Program.cs
AddSwaggerGen修改:
builder.Services.AddSwaggerGen(s => { s.SwaggerDoc("v1", new OpenApiInfo { Title = "MyDotNetCore", Description = "MyDotNetCore框架项目", Version = "v1" }); });
2、在项目新建目录source/css,放上一个自定义样式文件swagger-custom.css,启用静态文件访问:
app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory,"source")),//指定实际物理路径 RequestPath = new PathString("/source")//对外的访问路径 }); app.UseSwagger(); app.UseSwaggerUI(options => { options.InjectStylesheet("/source/css/swagger-custom.css"); });
3、每次生成要把source目录拷贝过去,打开MyDotNetCore.Project.WebApi.csproj,新增:
<ItemGroup> <Content Include="source\**"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup>