How to use Swagger Codegen with .net core

后端 未结 2 1768
南笙
南笙 2021-02-15 00:38

I am able to integrate the Swagge UI in my web api using Swashbuckle. I also want to explore the swagger codegen feature. Can somebody help in - how I can integrate swagger code

相关标签:
2条回答
  • 2021-02-15 01:29

    You should install "Swashbuckle.AspNetCore.Swagger" nuget package by right click your project and click manage nuget packages.

    Then you should add these codes into your project startup place eg. Program.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    
        // Register the Swagger generator, defining one or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
        });
    }
    
    public void Configure(IApplicationBuilder app)
    {
        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();
    
        app.UseMvc();
    }
    
    0 讨论(0)
  • 2021-02-15 01:35

    It seems like you just want to generate C# from the OpenApi specification (your Swagger implementation provides the input) of your API.

    To generate code (e.g. C#) from the OpenApi spec file of your API, you can do something like this:

    java -jar .\openapi-generator-cli-5.0.0-beta3.jar generate -i https://localhost:xxxx/api/v1/swagger.json -g csharp
    

    You have to download the OpenApi Generator Jar. Alternatively you can upload your code to a web generator. But I would always run this locally; you never know where your code ends up.

    0 讨论(0)
提交回复
热议问题