我有ASP.NET Core Web应用程序,在其中使用以下命令:
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();
}
当我导航到url / swagger时,出现以下错误:
失败:Microsoft.AspNetCore.Server.Kestrel [13] 连接ID为“ 0HLG1T7PRT05H”,请求ID为: 应用。 System.TypeLoadException:无法加载类型 来自的'Swashbuckle.AspNetCore.SwaggerGen.SwaggerResponseAttribute' 程序集'Swashbuckle.AspNetCore.SwaggerGen,Version = 3.0.0.0, 文化=中立 Swashbuckle.AspNetCore.Examples.DescriptionOperationFilter.SetResponseModelDescriptions(Operation 操作,ISchemaRegistry schemaRegistry,ApiDescription apiDescription) Swashbuckle.AspNetCore.Examples.DescriptionOperationFilter.Apply(Operation 操作,在OperationFilterContext上下文中) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreateOperation(ApiDescription apiDescription,ISchemaRegistry schemaRegistry),网址为 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable
1 apiDescriptions, ISchemaRegistry schemaRegistry) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable
1 来源,Func2 keySelector, Func
2 elementSelector, IEqualityComparer1 comparer) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItems(IEnumerable
1 apiDescriptions,ISchemaRegistry schemaRegistry),网址为 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName,字符串主机,字符串basePath,字符串[]方案),位于 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(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
答案 0 :(得分:4)
这对我们有用,同时升级到.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)");
});
...
}
基本上,以下是在https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v5.0.0-rc4找到的示例
答案 1 :(得分:2)
卸载程序包
Swashbuckle.AspNetCore.Examples
应解决此问题。新软件包是(尚未尝试过)-
Swashbuckle.AspNetCore.Filters
(更新) 新程序包可以正常运行
答案 2 :(得分:0)
这与版本3中的一些重大更改有关。Here you can find more information.
要解决此问题,请尝试添加Swashbuckle.AspNetCore.Annotations package.
ForeignKey
对于dotnet核心:
@Override
public void onBindViewHolder(@NonNull viewHolder holder, final int position) {
holder.abstractText.setText(events.get(position).abstractTExt);
Rect bounds = new Rect();
Paint textPaint = holder.abstractText.getPaint();
textPaint.getTextBounds((String) holder.abstractText.getText(),0,holder.abstractText.getText().length(),bounds);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, bounds.height());
holder.borderImage.setLayoutParams(layoutParams);
Log.d("-------------borderHe", String.valueOf((holder.abstractText.getLineHeight() * holder.abstractText.getLineCount())));
}
答案 3 :(得分:0)
将Swashbuckle.AspNetCore回滚到v2.5.0可以达到目的
答案 4 :(得分:0)
要使其在更高版本和ASPNET Core中运行,您需要:
AddSwaggerGen
调用ExampleFilters();
中(在ConfigureServices中)IExampleProvider<TargetDtoType>
实现示例类,其中TargetDtoType
是示例的目标类型AddSwaggerExamplesFromAssemblyOf<ExampleTypeImplementation>
,其中ExampleTypeImplementation是步骤3中实现IExampleProvider<TargetDtoType>
的类型这帮了我大忙。不需要任何动作方法属性,swashbuckle通过参数类型拾取并自动生成示例