Edi*_*son 1 swagger .net-core-3.0
我正在使用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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的创业班
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();
});
}
Run Code Online (Sandbox Code Playgroud)
有人可以照亮,在此先感谢
这是为您的项目添加招摇的最少步骤。
Install-Package Swashbuckle.AspNetCore -Version 5.5.1
Run Code Online (Sandbox Code Playgroud)
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
Run Code Online (Sandbox Code Playgroud)
// 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");
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
629 次 |
| 最近记录: |