swagger.json路径和定义为空。规范中未定义任何操作

时间:2019-01-12 13:21:32

标签: c# swagger asp.net-core-2.1 .net-core-2.1

我正在开发.netcore Web应用程序。我正在使用招摇工具,并且已经进行了所有必要的调整。不幸的是,它不起作用,我只能在the展输出页面中看到No operations defined in spec!

带有/swagger/v1/swagger.json的swagger文件具有以下内容:

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "Something"
  },
  "paths": {},
  "definitions": {}
}

我想在摇摇欲坠的输出页面中查看我的控制器及其动作。

2 个答案:

答案 0 :(得分:0)

经过一番研究,我发现我的问题是关于在.NetCore2.1中将swagger与OData一起使用。 我找到了解决此问题的方法。

首先,我添加了以下两个Nuget软件包:

Swashbuckle.AspNetCore
Swashbuckle.AspNetCore.Annotations

然后,我在Startup.cs中添加了以下代码

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);

该,我在控制器中添加了以下代码行:

[ApiExplorerSettings(IgnoreApi = false)] 

请注意,它对我有用,但可能需要更多研究以最终发现副作用

答案 1 :(得分:-1)

您需要在XML Documentation file标签下启用project obtions => Build

然后,您需要通过swagger读取此文件,以便swagger可以从中创建文档。

private static string[] XmlCommentsFilePath
{
    get
    {
        var basePath = PlatformServices.Default.Application.ApplicationBasePath;

        var apiDocFile = typeof(Startup).GetTypeInfo().Assembly.GetName().Name + ".xml";
        var apiPath = Path.Combine(basePath, apiDocFile);

        return new[] {apiPath};

    }
}

在ConfigureServices

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)));

    ....
});