【问题标题】:Incorrect link in SwaggerSwagger 中的链接不正确
【发布时间】:2018-11-13 10:56:01
【问题描述】:

我正在使用带有 Swashbuckle.AspNetCore (2.4.0) 的 ASP.NET Core 2.0

在启动配置服务中

services.AddSwaggerGen(c =>
        {
            c.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
         c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "Library API",
                Description = "ASP.NET Core Web API",
                TermsOfService = "None",
                Contact = new Contact
                {
                    Name = "my name",
                    Email = "myname@mywebsite"
                }
            });
        });
      });

在启动配置中

          app.UseSwagger(c =>
        {
            c.RouteTemplate =
                "api-docs/{documentName}/swagger.json";
        });
        app.UseSwaggerUI(c =>
        {
            c.RoutePrefix = "api-docs";
            c.SwaggerEndpoint("v1/swagger.json", "Api v1");
        });

 public class AuthorizationHeaderParameterOperationFilter : IOperationFilter
    {
        public void Apply(Operation operation, OperationFilterContext context)
        {
            var filterPipeline = context.ApiDescription.ActionDescriptor.FilterDescriptors;
            var isAuthorized = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is AuthorizeFilter);
            var allowAnonymous = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is IAllowAnonymousFilter);

            if (isAuthorized && !allowAnonymous)
            {
                if (operation.Parameters == null)
                    operation.Parameters = new List<IParameter>();

                operation.Parameters.Add(new NonBodyParameter
                {
                    Name = "Authorization",
                    In = "header",
                    Description = "access token",
                    Required = true,
                    Type = "string"
                });
            }
        }
    }

看起来像这样

但是链接会转到网址

about:blank

如何将 v1/swagger.json 链接和服务条款链接设置到正确的位置?

【问题讨论】:

  • 我也有同样的问题。我认为这是一个错误。如果您在 swagger 端点中使用相对路径(没有“/”),您将得到这样的断开链接。但是 swagger.json 可用并且一切正常。
  • 这就是答案。 c.SwaggerEndpoint("./v1/swagger.json", "Api v1");作品。介意写出来吗? @Alexey.Petriashev
  • 联系链接现在也有效,但服务条款无效。也许那是因为我遗漏了许可证信息。
  • 我不确定我的评论对你有帮助)

标签: asp.net-core swagger swashbuckle


【解决方案1】:

使用c.SwaggerEndpoint("./v1/swagger.json", "Api v1");

相对路径(不带“/”)呈现空链接但 swagger.json 可用并且一切正常,这看起来像是一个错误。

我使用相对路径与反向代理一起正常工作,但这是另一回事......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多