Swagger UI显示“未找到提取错误”

时间:2018-10-18 10:20:26

标签: swagger asp.net-core-2.1

在我的asp.net core 2.1 api中,我添加了服务和Swashbuckle的静态文件,但显然找不到生成的.json

services.AddSwaggerGen(x =>
    x.SwaggerDoc("Swagger", new Info { Title = "Asp.Net Core 2 Api", Description = "Swagger Core Api" }));
----
app.UseCors("AllowAll");
app.UseAuthentication();
app.UseStaticFiles();

app.UseSwagger();
app.UseSwaggerUI(x => x.SwaggerEndpoint("/swagger/swagger.json", "Core Api"));

enter image description here

有什么主意吗?谢谢

4 个答案:

答案 0 :(得分:3)

您的摇摇晃晃的端点具有错误的网址,因此出现“未找到”错误。 通过默认方式构造的Swagger文档的网址格式为/swagger/<document-name>/swagger.json,其中{{1}的调用中提供了 name (第一个字符串)参数}

在您的情况下,您提供了“ Swagger”,因此端点URL应该为x.SwaggerDoc("Swagger", new Info {...});

在您的ConfigureServices中,您的呼叫应类似于

"/swagger/Swagger/swagger.json"

答案 1 :(得分:0)

在IIS上发布应用程序后, 我遇到了同样的问题,这是我解决的方法。

Startup.cs文件中,添加public void ConfigureServices(IServiceCollection services)...方法

services.AddSwaggerGen();
services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("OpenAPISpec",
        new OpenApiInfo()
        {
            Title = "Your Title here",
            Version = "1",
            Description = "A simple example ASP.NET Core Web API",
            TermsOfService = new Uri("https://example.com/terms"),
            Contact = new OpenApiContact()
            {
                Email = "youremail@gmail.com",
                Name = "Jean Fritz DUVERSEAU",
                Url = new Uri("https://www.rezo509.com")
            }
        });

    var xmlCommentFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    var xmlCommentsFullPath = Path.Combine(AppContext.BaseDirectory, xmlCommentFile);
    options.IncludeXmlComments(xmlCommentsFullPath);
});

,然后使用public void Configure(...方法

app.UseSwagger();
app.UseSwaggerUI(s =>
{
    string swaggerPath = string.IsNullOrWhiteSpace(s.RoutePrefix) ? "." : "..";
    s.SwaggerEndpoint($"{swaggerPath}/swagger/OpenAPISpec/swagger.json", "Demo API");
});

执行完项目后,然后在浏览器中按如下所示修改URL:

http(s)://yourserver.com:[端口] /swagger/index.html enter image description here

如果您没有添加正确的网址,您将获得HTTP ERROR 404 enter image description here

并且在IIS服务器上发布后 enter image description here

答案 2 :(得分:0)

长话短说:您必须检查环境中路径前缀的差异,并在SwaggerEndpoint([different path])选项中反映此差异。 方便的方法之一是自动执行此区别,方法是将此选择放到#if DEBUG部分

答案 3 :(得分:0)

这对我有用 `

public void ConfigureServices(IServiceCollection services)
{
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo
        {
            Title = "Test API",
            Version = "v1",
            Description = "List of Api's.",
            Contact = new OpenApiContact
            {
                Name = "Test site",
                Email = string.Empty
            },
        });
    });
    services.AddCors();
    services.AddHttpContextAccessor();
    services.AddSingleton<Microsoft.AspNetCore.Http.IHttpContextAccessor, HttpContextAccessor>();
    services.AddControllers();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseSwagger();

    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("v1/swagger.json", "  API V1");
        c.RoutePrefix = "swagger";
    });


    app.UseRouting();

    app.UseAuthorization();

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

`


尝试从浏览器复制粘贴您的应用程序链接 例如http://localhost/testsite/swagger/v1/swagger.json 将其与错误中的链接进行比较。 相应地调整 c.SwaggerEndpoint 中的 url