Swagger UI 显示 "Fetch error Not Found"

Swagger UI showing "Fetch error Not Found"

在我的 asp.net core 2.1 api 我添加了静态文件 serving 和 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"));

有什么想法吗?谢谢

您的 swagger 端点错误 Url,因此出现 'Not Found' 错误。 通过默认方式构建的 Swagger 文档的 url 格式为 /swagger/<document-name>/swagger.json 其中 name (first string)调用 x.SwaggerDoc("Swagger", new Info {...});

时提供的参数

在您的情况下,您提供了 "Swagger",因此您的端点 url 应该是 "/swagger/Swagger/swagger.json"

在您的 ConfigureServices 中,您的调用应该是这样的

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

在 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

如果您没有添加正确的 url,您将得到 HTTP ERROR 404

并且在 IIS 服务器上发布之后

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

这对我有用 `

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 无效配置(IApplicationBuilder 应用程序,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();
    });
}

`


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