Yah*_*ein 9 c# swagger asp.net-core
我有 ASP.NET Core Web 应用程序,我在其中使用 swagger 使用以下内容:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSwaggerGen(c =>
{
c.OperationFilter<ExamplesOperationFilter>();
c.OperationFilter<DescriptionOperationFilter>();
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "API",
Description = "",
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSwagger();
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "API"); });
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
当我导航到 url/swagger 时,出现以下错误:
失败:Microsoft.AspNetCore.Server.Kestrel[13] 连接 ID“0HLG1T7PRT05H”,请求 ID:应用程序引发了未处理的异常。System.TypeLoadException: 无法从程序集 'Swashbuckle.AspNetCore.SwaggerGen, Version=3.0.0.0, Culture=neutral'.at Swashbuckle.AspNetCore.Examples.DescriptionOperationFilter.SetResponseModelDescriptions 加载类型 'Swashbuckle.AspNetCore.SwaggerGen.SwaggerResponseAttribute' , ISchemaRegistry schemaRegistry, ApiDescription apiDescription) at Swashbuckle.AspNetCore.Examples.DescriptionOperationFilter.Apply(Operation operation, OperationFilterContext context) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreateOperation(ApiDescription apiDescription, ISchemaRegistryCorenetschemaRegistry.Asp)
1 apiDescriptions, ISchemaRegistry schemaRegistry) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 个源,Func2 keySelector, Func2 elementSelector,IEqualityComparer1 comparer) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItems(IEnumerable1 apiDescriptions,ISchemaRegistry schemaRegistry) 在 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath, String[] 方案) 在 Swashbuckle.AspNetCore.Swaggerware.SwaggerMiddle(M) HttpContext httpContext) 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 应用程序)
我安装的nuget包是:
Swashbuckle.AspNetCore v3.0.0
Swashbuckle.AspNetCore.Examples v2.9.0
Ieu*_*anW 11
卸载软件包
Swashbuckle.AspNetCore.Examples
Run Code Online (Sandbox Code Playgroud)
应该解决这个问题。新包是(还没试过)-
Swashbuckle.AspNetCore.Filters
Run Code Online (Sandbox Code Playgroud)
(更新) 新包运行良好
这对我们有用,同时升级到 .netcore 3.0:
1) 安装包 Swashbuckle.AspNetCore -Version 5.0.0-rc4
2)将代码更改为
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPI", Version = "v1" });
});
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,ILoggerFactory loggerFactory)
{
...
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "swagger/ui";
c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI(v1)");
});
...
}
Run Code Online (Sandbox Code Playgroud)
基本上,以下示例位于https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v5.0.0-rc4