简体   繁体   English

swagger.json 路径和定义为空。 规范中没有定义操作

[英]swagger.json paths and definitions are empty. No operations defined in spec

I am developing a .netcore web application.我正在开发一个 .netcore Web 应用程序。 I am using of swagger and I've made all the necessary adjustments.我正在使用 swagger 并进行了所有必要的调整。 Unfortunately it does not work and I just see No operations defined in spec!不幸的是它不起作用,我只看到No operations defined in spec! in the swagger output page.在 swagger 输出页面中。

The swagger file with /swagger/v1/swagger.json has the following content:带有/swagger/v1/swagger.json的 swagger 文件具有以下内容:

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

I want to see my controllers and their actions in swagger output page.我想在 swagger 输出页面中查看我的控制器及其操作。

after some research i was found that my problem was about using swagger along with OData in .NetCore2.1. 经过一番研究,我发现我的问题是关于在.NetCore2.1中将swagger与OData一起使用。 i found a solution for this problem. 我找到了解决这个问题的方法。

first i added two following Nuget packages: 首先,我添加了以下两个Nuget软件包:

Swashbuckle.AspNetCore
Swashbuckle.AspNetCore.Annotations

then, i added following codes in Startup.cs 然后,我在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);

the, i added following code line in controllers: 的,我在控制器中添加了以下代码行:

[ApiExplorerSettings(IgnoreApi = false)] 

please note that it was worked for me but it may be need more research for eventually side effects 请注意,它对我有用,但可能需要更多研究以最终发现副作用

Please add a in contoller any method like a then showing swagger methods请在控制器中添加任何方法,例如然后显示 swagger 方法

  [HttpGet]
    [EnableQuery]
    public IQueryable<int> Get()
    {
        return 1;
    }

Sounds like you need to add some routes in the controller.听起来您需要在控制器中添加一些路由。 With rest services you will need to add them separate from what was built automatically by Visual Studio.对于休息服务,您需要将它们与 Visual Studio 自动构建的内容分开添加。 for example // GET: Items [HttpGet] [Route("/items")] ...rest of function... this will give your swagger a reference to what it does when you click the button.例如 // GET: Items [HttpGet] [Route("/items")] ...其余的功能...这会给你的招摇参考当你点击按钮时它会做什么。

you need to enable XML Documentation file under project obtions => Build tab. 您需要在project obtions => Build选项卡下启用XML Documentation file

Then you need to read this file through swagger so that swagger can create documentation from it. 然后,您需要通过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};

    }
}

In ConfigureServices 在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)));

    ....
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按控制器名称对 NSwag swagger.json 中的路径进行排序/排序 - How to order/sort paths in NSwag swagger.json by controller name Swagger 不生成 swagger.json - Swagger is not generating swagger.json 显示 Swagger.json 但 UI 不显示 - Swagger.json is displayed but the UI not 使用 GenerateSchema 添加的类出现在 Swagger UI 中,但不在 swagger.json 中 - Classes Added Using GenerateSchema Appear in the Swagger UI But Not in swagger.json 当本地主机提供 swagger.json 时,Autorest 无法针对文件解析 swagger.json - Autorest Failed resolving swagger.json against file when swagger.json is served by localhost Swagger 'swagger.json' 加载,但在 AspNet 项目中的 swagger UI '{localhost}/swagger' 上出现 404 错误 - Swagger 'swagger.json' loads, but 404 error on swagger UI '{localhost}/swagger' in AspNet project 如何修复 .net core 2.2 应用程序中未找到的 swagger.json - How to fix swagger.json not found in .net core 2.2 app ASP.NET Core - Swashbuckle 未创建 swagger.json 文件 - ASP.NET Core - Swashbuckle not creating swagger.json file 我没有在 swagger.json 中得到方法描述 - I'm not getting a method description in swagger.json Generating C# Rest API Client from swagger.json - Generating C# Rest API Client from swagger.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM