0
我正在嘗試爲我的.Netcore應用程序(1.1)配置Swagger,並且無法生成文檔。Swagger .Net Core
這裏是我的配置
public void ConfigureServices(IServiceCollection services) {
services.AddMvcCore().AddVersionedApiExplorer(o => o.GroupNameFormat = "1.0");
services.AddMvc();
services.AddApiVersioning(opt =>
{
opt.ApiVersionReader = new HeaderApiVersionReader("api-version");
opt.DefaultApiVersion = new ApiVersion(1, 0);
opt.ReportApiVersions = true;
opt.AssumeDefaultVersionWhenUnspecified = true;
});
services.AddSwaggerGen(
options =>
{
options.SwaggerDoc("1.0",new Info {Contact = new Contact() {Name="Admin" } });
// add a custom operation filter which sets default values
options.OperationFilter<SwaggerDefaultValues>();
});
}
在配置方法
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=User}/{action=Get}/{requestString?}");
});
app.UseSwagger(o=>
{
o.RouteTemplate = "docs/{documentName}/swagger.json";
});
app.UseSwaggerUI(
options =>
{
options.SwaggerEndpoint("/docs/1.0/swagger.json", "1.0");
});
當我運行應用程序, http://localhost:5000/docs/1.0/swagger.json
我得到了下面的方法,我的API中沒有一個是發現。
{"swagger":"2.0","info":{"contact":{"name":"Admin"}},"basePath":"/","paths":{},"definitions":{},"securityDefinitions":{}}
1.您是否使用相同的[SwaggerDefaultValues](https://github.com/Microsoft/aspnet-api-versioning/blob/master/samples/aspnetcore/SwaggerSample/SwaggerDefaultValues.cs)過濾器?如果不是,請分享。 – Set
@Set,我正在關注此示例以設置swagger https://github.com/Microsoft/aspnet-api-versioning/blob/master/samples/aspnetcore/SwaggerSample/Startup.cs 我已經設置了SwaggerDefaultValues篩選器。 –
我對addMvc發表了評論,並將我的代碼配置爲使用addMVCCore,但仍然存在相同的問題:services.AddMvcCore()。 AddControllersAsServices()。 AddJsonFormatters()。 AddJsonOptions(優化=> { 如果(opt.SerializerSettings.ContractResolver = NULL){ opt.SerializerSettings.ContractResolver =新DefaultContractResolver();! } })。 AddVersionedApiExplorer(o => o.GroupNameFormat =「1.0」);' –