我正在使用swagger记录我的Web api。但是,当我运行我的项目时,它将引发错误“找不到方法:'无效Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware ...”。
我只关注youtube上的教程。不知道是什么原因造成的。
我的项目是.Net Core 3。
internal class SwaggerConfiguration
{
public static void Configure(IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info() { Title = "Sample Portal API", Version = "1.0.0.0" });
});
}
public static void Configure(IConfiguration configuration, IApplicationBuilder app)
{
var swaggerOptions = new SwaggerOptions(configuration).Bind();
app.UseSwagger(options =>
{
options.RouteTemplate = swaggerOptions.Route;
});
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint(swaggerOptions.UIEndpoint, swaggerOptions.Description);
});
}
private class SwaggerOptions
{
IConfiguration _configuration;
public SwaggerOptions(IConfiguration configuration)
{
_configuration = configuration;
}
public string Route { get; set; }
public string Description { get; set; }
public string UIEndpoint { get; set; }
public SwaggerOptions Bind()
{
_configuration.GetSection(nameof(SwaggerOptions)).Bind(this);
return this;
}
}
}
这是我的创业班
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
SwaggerConfiguration.Configure(services);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
SwaggerConfiguration.Configure(Configuration, app);
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
有人可以照亮,谢谢!
答案 0 :(得分:5)
答案 1 :(得分:3)
这是向您的项目中添加摇摇欲坠的最少步骤。
Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc4
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
})
答案 2 :(得分:0)
我遇到了这个问题,我要做的就是将Nuget软件包Swashbuckle.AspNetCore更新到5.4.1版,或者如果没有的话安装它。