【问题标题】:Swagger stopped working招摇停止工作
【发布时间】:2018-09-02 15:54:56
【问题描述】:

在一个 asp.net core 2 web.api 项目中,我使用本教程添加了 Swagger: https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?tabs=visual-studio%2Cvisual-studio-xml

效果很好

然后我不得不重新运行该函数以从实体框架 db-first 构建模型,在我在 swagger gui 中遇到此错误之后:

No operations defined in spec! 

我已经删除了一个已读的招摇,但没有改变。下载

/swagger/v1/swagger.json

看起来像这样:

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

Startup.cs:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using SmoEventApi.Models;
using Swashbuckle.AspNetCore.Swagger;

#region AddedUsings

using Microsoft.EntityFrameworkCore;
#endregion

namespace SmoEventApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        #region ConfigureServices
        // This method gets called by the runtime. Use this method to add services to the container.
        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" });
            });

            var connection = @"Server=xxxx\MSSQL01;Database=yyy;User Id=user;Password=pp;ConnectRetryCount=0";
            services.AddDbContext<DCMMContext>(options => options.UseSqlServer(connection));


        }
        #endregion

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            // 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.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

我不知道为什么我不再从 swagger 获得任何信息?

卡雷

【问题讨论】:

  • 你能用Startup 类中的Configure 和ConfigureServices 方法的内容更新你的问题吗?

标签: asp.net .net asp.net-core swagger


【解决方案1】:

原来是控制器中缺少的注释

【讨论】:

  • 对于遇到此问题的任何其他人。我需要将[ApiController] 添加到控制器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多