swagger.json paths and definitions are empty. No operations defined in spec
我正在开发.netcore Web应用程序。 我正在使用招摇工具,并且已经进行了所有必要的调整。 不幸的是,它不起作用,我只能在svagger输出页面中看到
具有
1 2 3 4 5 6 7 8 9 | { "swagger":"2.0", "info": { "version":"v1", "title":"Something" }, "paths": {}, "definitions": {} } |
我想在摇摇欲坠的输出页面中看到我的控制器及其动作。
经过一番研究,我发现我的问题是关于在.NetCore2.1中使用swagger和OData。
我找到了解决这个问题的方法。
首先,我添加了以下两个Nuget软件包:
1 2 | Swashbuckle.AspNetCore Swashbuckle.AspNetCore.Annotations |
然后,我在Startup.cs中添加了以下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | services.AddMvc(options => { foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0)) { outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata")); } foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0)) { inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata")); } }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1); |
的,我在控制器中添加了以下代码行:
1 | [ApiExplorerSettings(IgnoreApi = false)] |
请注意,它对我有用,但可能需要更多研究以最终发现副作用
您需要在
然后,您需要通过swagger读取此文件,以便swagger可以从中创建文档。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
在ConfigureServices中
1 2 3 4 5 6 7 8 9 | services.AddSwaggerGen(options => { var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>(); // add a swagger document for each discovered API version provider.ApiVersionDescriptions.ForEach(x => options.SwaggerDoc(x.GroupName, CreateInfoForApiVersion(x))); .... }); |