为什么我要查看JSON,而不是漂亮的Swagger UI?

时间:2019-01-18 10:49:03

标签: asp.net-core swagger swashbuckle

these instructions之后,我已经在一个干净的asp.net核心Web api项目上安装了swashbuckle。我的启动课程如下。您可以看到我已经添加了AddSwaggerGen()UseSwagger()UseSwaggerUI()

当我访问https://localhost:44334/swagger/v1/swagger.json时,没有看到我期望的摇摇欲坠的UI,而是堆了一堆JSON,从{"swagger":"2.0","info":{"version":"v1","title":"MoqOcr"}...开始

我想念什么?

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

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        // sby
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "MoqOcr", Version = "v1" });
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        // sby
        // 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");
            c.RoutePrefix = string.Empty;

        });

        //app.UseHttpsRedirection();
        app.UseMvc();
    }
}

1 个答案:

答案 0 :(得分:0)

我已经在Startup.cs中检查了Swagger的配置,似乎没有意外的事情来设置Swagger。我唯一想到的是您误解了SwaggerEndpoint设置,该设置向您指示(我想)您可以从该URL访问Swagger UI,但是它包含一个json以构建和配置该UI页面。足够公平,但您应该尝试https://localhost:44334/swaggerhttps://localhost:44334/swagger/index.html查看您的Swagger UI页面。希望这能解决您的问题。