简体   繁体   English

ASP.NET Core Web API - Fetch error undefined /swagger/MyApp API v1/swagger.json

[英]ASP.NET Core Web API - Fetch error undefined /swagger/MyApp API v1/swagger.json

I developed ASP.NET Core-5 Web API AND ALSO IMPLEMENTED Swagger in it:我开发了 ASP.NET Core-5 Web API 并在它:

startup.cs:启动.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddVersioning();
        services.AddSwagger();
        ...
    }


    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider, IApiVersionDescriptionProvider provider,)
    {
        app.UseDeveloperExceptionPage();
        app.UseStatusCodePagesWithReExecute("/errors/{0}");
        app.UseVersionedSwagger(provider);
    ...
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
               name: "default",
               pattern: "{controller}/{action=Index}/{id?}");
        });
    }

Swagger: Swagger:

    public static IServiceCollection AddVersioning(this IServiceCollection services)
    {
        services.AddApiVersioning(
            options =>
            {
                options.ReportApiVersions = true;
            });
        services.AddVersionedApiExplorer(
            options =>
            {
                options.GroupNameFormat = "'MyApp API v'VVV";

                options.SubstituteApiVersionInUrl = true;
            });

        return services;
    }


    public static IServiceCollection AddSwagger(this IServiceCollection services)
    {
        services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();

        services.AddSwaggerGen(options =>
        {
            options.OperationFilter<SwaggerDefaultValues>();

            options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
            {
                Scheme = "Bearer",
                Description = @"JWT Authorization header using the Bearer scheme. <br><br> 
                  Enter 'Bearer' [space] and then your token in the text input below.
                  <br><br>Example: 'Bearer 12345abcdef'",
                Name = "Authorization",
                In = ParameterLocation.Header,
                Type = SecuritySchemeType.Http
            });

            options.OperationFilter<SwaggerAuthorizeCheckOperationFilter>();

            // integrate xml comments
            options.IncludeXmlComments(XmlCommentsFilePath);
        });

        return services;
    }


    public static IApplicationBuilder UseVersionedSwagger(this IApplicationBuilder app, IApiVersionDescriptionProvider provider)
    {
        app.UseSwagger();

        app.UseSwaggerUI(
            options =>
            {
                // build a swagger endpoint for each discovered API version
                foreach (var description in provider.ApiVersionDescriptions)
                {
                    options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
                }
            });

        return app;
    }

Everything worked fine on the local system, but when I deployed to IIS on the Remote server.在本地系统上一切正常,但是当我部署到远程服务器上的 IIS 时。

Then I tried to test:然后我尝试测试:

https://localhost:8443/myapp/ https://localhost:8443/myapp/

gives:给出:

No webpage was found for the web address: https://localhost:8443/myapp/没有找到 web 地址的网页:https://localhost:8443/myapp/

while尽管

https://localhost:8443/myapp/swagger/index.html https://localhost:8443/myapp/swagger/index.html

gives:给出:

I got this error:我收到了这个错误:

Fetch error undefined /swagger/MyApp API v1/swagger.json获取错误未定义 /swagger/MyApp API v1/swagger.json

How do I resolve this?我该如何解决这个问题?

Thanks谢谢

Try to add the following code in the Configure method in the startup.cs file:尝试在startup.cs文件的Configure方法中添加如下代码:

if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseSwagger(c =>
        {
            c.RouteTemplate = "/swagger/{documentName}/swagger.json";
        });
        app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"));
    }

Then add the following code in the ConfigureServices method:然后在 ConfigureServices 方法中添加如下代码:

services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
        c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
    });

RouteTemplate is used here, the difference between it and the SwaggerEndpoint string.这里用到了RouteTemplate,它和SwaggerEndpoint字符串的区别。 One uses {documentName} and the other uses "v1" as the text.一个使用 {documentName},另一个使用“v1”作为文本。

Specific reference documents:具体参考文件:

https://www.thecodebuzz.com/resolved-failed-to-load-api-definition-undefined-swagger-v1-swagger-json/ https://www.thecodebuzz.com/resolved-failed-to-load-api-definition-undefined-swagger-v1-swagger-json/

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

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