前言:项目也像人一样,不能总穿一套衣服,也是需要换洗的。(嘻嘻,在这里小弟很佩服前辈的脑子)。
官网实战指南:https://doc.xiaominfo.com/knife4j/action/dotnetcore-knife4j-guid.html
下面进入主题:替换项目中【swagger组件】皮肤,使用IGeekFan.AspNetCore.Knife4jUI皮肤。
第一步:在原有的项目上安装包
Install-Package Swashbuckle.AspNetCore.Swagger
Install-Package Swashbuckle.AspNetCore.SwaggerGen
Install-Package IGeekFan.AspNetCore.Knife4jUI
第二步: 服务配置
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v2", new OpenApiInfo { Title = "Liuliu.Blogs.Web", Version = "v2" });
c.AddServer(new OpenApiServer()
{
Url = "",
Description = "vvv"
});
c.CustomOperationIds(apiDesc =>
{
var controllerAction = apiDesc.ActionDescriptor as ControllerActionDescriptor;
return controllerAction.ControllerName + "-" + controllerAction.ActionName;
});
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "Liuliu.Blogs.Web.xml"), true);
});
第三步:中间件配置
app.UseSwagger();
//app.UseMiddleware<HostHttpCryptoMiddleware>();
//app.UseMiddleware<JsonNoFoundHandlerMiddleware>();
app.UseMiddleware<JsonExceptionHandlerMiddleware>();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseOSharp();
app.UseKnife4UI(c =>
{
c.RoutePrefix = ""; // serve the UI at root
c.SwaggerEndpoint("/v1/api-docs", "Liuliu.Blogs.Web V1");
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapSwagger("{documentName}/api-docs");
});
效果图