简体   繁体   English

Swagger 不工作 Asp.net 内核如何打开 swagger ui

[英]Swagger is not Working Asp.net Core how to open swagger ui

This is my Startup.cs file这是我的Startup.cs文件

This is my ConfigureService method in Startup.cs.这是我在 Startup.cs 中的ConfigureService方法。 I have modified it exactly according to documentation, but it's not working.我已经完全根据文档对其进行了修改,但它不起作用。 I have removed the launch Url, so it's just going on the port and I have not set any routing.我已经删除了启动 Url,所以它只是在端口上进行,我没有设置任何路由。

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddControllers();
            services.ConnectionToACQEs(Configuration);
            services.AddAutoMapper(typeof(Startup));
            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "ToDo API",
                    Description = "A simple example ASP.NET Core Web API",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact = new OpenApiContact
                    {
                        Name = "Nicky Liu",
                        Email = "nicky@zedotech.com",
                        Url = new Uri("https://www.zedotech.com"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url = new Uri("https://example.com/license"),
                    }
                });
            });
        }
    

This is my Configure method:这是我的Configure方法:

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
       {
           //if (env.IsDevelopment())
           //{
           //    app.UseDeveloperExceptionPage();
           //}


           /// Enable middleware to serve generated Swagger as a JSON endpoint.
           app.UseSwagger();
           // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
           // specifying the Swagger JSON endpoint.
           app.UseSwaggerUI(c =>
           {
               c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
           });
           //app.UseHttpsRedirection();

           app.UseRouting();

           //app.UseAuthorization();

           app.UseEndpoints(endpoints =>
           {
               endpoints.MapControllers();
           });



       }

This is my Startup.cs file这是我的 Startup.cs 文件

This is my ConfigureService method in Startup.cs i have modified it exactly according to documentation but its not working i have removed the launch Url SO its just going on the port and i have not set any routing i am having a trouble with his help would be aprreciated这是我在 Startup.cs 中的 ConfigureService 方法,我已经完全根据文档对其进行了修改,但它不起作用我已经删除了启动 Url 所以它只是在端口上,我没有设置任何路由我在他的帮助下遇到了麻烦被欣赏

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddControllers();
            services.ConnectionToACQEs(Configuration);
            services.AddAutoMapper(typeof(Startup));
            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "ToDo API",
                    Description = "A simple example ASP.NET Core Web API",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact = new OpenApiContact
                    {
                        Name = "Nicky Liu",
                        Email = "nicky@zedotech.com",
                        Url = new Uri("https://www.zedotech.com"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url = new Uri("https://example.com/license"),
                    }
                });
            });
        }
    

This is my Configure Method这是我的配置方法

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
       {
           //if (env.IsDevelopment())
           //{
           //    app.UseDeveloperExceptionPage();
           //}


           /// Enable middleware to serve generated Swagger as a JSON endpoint.
           app.UseSwagger();
           // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
           // specifying the Swagger JSON endpoint.
           app.UseSwaggerUI(c =>
           {
               c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
           });
           //app.UseHttpsRedirection();

           app.UseRouting();

           //app.UseAuthorization();

           app.UseEndpoints(endpoints =>
           {
               endpoints.MapControllers();
           });



       }

Try cleaning your build and building your project.尝试清理您的构建并构建您的项目。

If it still doesn't work, make sure that your controller has no method without ActionVerbs, ie: HttpGet, HttpPost and so.如果还是不行,请确认你的controller没有没有ActionVerbs的方法,即: HttpGet, HttpPost等。 Swagger requires all controller methods to have ActionVerbs, except those with [ApiExplorerSettings(IgnoreApi = true)] attribute. Swagger 要求所有 controller 方法具有 ActionVerbs,但具有[ApiExplorerSettings(IgnoreApi = true)]属性的方法除外。

None of your methods should define the route along with the ActionVerb: Do [HttpGet,Route("getuser")] instead of [HttpGet("getuser")]您的任何方法都不应与 ActionVerb 一起定义路由:Do [HttpGet,Route("getuser")]而不是[HttpGet("getuser")]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM