【问题标题】:ASP.NET Core RC1 - WebAPI Swagger Integration - "Error" SchemaValidationMessagesASP.NET Core RC1 - WebAPI Swagger 集成 - “错误”SchemaValidationMessages
【发布时间】:2017-01-31 19:44:04
【问题描述】:

我已经在我的 ASP.NET Core RC1 应用程序中集成了 swagger 以及以下 NuGet 包。

"Swashbuckle.SwaggerGen": "6.0.0-rc1-final",
"Swashbuckle.SwaggerUi": "6.0.0-rc1-final"

这是 swagger 集成的代码。

    public void ConfigureServices(IServiceCollection services)
    {
        ....
        .....

        //*** Configure Swagger Document Information.
        services.ConfigureSwaggerDocument(options =>
        {
            //*** Define incremental API version.
            options.SingleApiVersion(new Info
            {
                Version = "v1",
                Title = "TEST APIs",
                Description = "Test API Methods",
                TermsOfService = "",
            });


            //*** Assign the API - Swagger helper document.
            options.OperationFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlActionComments(helperDocPath));

        });

        //*** Configure the Swagger schema settings.
        services.ConfigureSwaggerSchema(options =>
        {
            options.DescribeAllEnumsAsStrings = true;
            options.ModelFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlTypeComments(helperDocPath));
        });

       ....
       ....
    }

    //**** Configure Method

    private void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        ...
        ....

        //*** Add Swagger pluggins to application environment.
        app.UseSwaggerGen();
        app.UseSwaggerUi();
    }

代码生成 swagger 文档,同时使用 localhost -> "http://localhost:8080/testapiproject/swagger/ui/index.html" 在本地访问它。

但是,在部署服务器中部署代码后,我仍然会收到 swagger 文档,但我在底部收到“错误”,点击后显示,

{"schemaValidationMessages":[{"level":"error","message":"Can't read from file http://<applicationdomainname>:8080//testapiproject/swagger/v1/swagger.json"}]}.

【问题讨论】:

  • http://:8080//testapiproject/swagger/v1/swagger.json 这条路径似乎是错误的。 hostname 不见了。您正在尝试从错误的域获取 json。
  • 本地主机以外的任何内容,底部显示错误,这是架构验证错误。

标签: c# asp.net-core swagger


【解决方案1】:

我认为它试图从错误的路径访问json 数据。确保在swaggeruitestapiproject/swagger/ui/index.html"中正确配置了路径

 $(function() {
        var url = window.location.search.match(/url=([^&]+)/);
        if (url && url.length > 1) {
            url = decodeURIComponent(url[1]);
        } else {
            url = "/swagger/docs/v1";     // Make sure it's not hardcoded
        }
}

为 .Net Core 更新

我只安装了"Swashbuckle": "6.0.0-beta902" 包。

   public void ConfigureServices(IServiceCollection services)
{
            services.AddSwaggerGen();
            services.ConfigureSwaggerGen(options =>
            {   
                options.SingleApiVersion(new Info
                {
                    Version = "v1",
                    Title = "test API",
                    Description = "test api for swagger",
                    TermsOfService = "None",

                });
                options.IncludeXmlComments(/* whatever tha path */);
                options.DescribeAllEnumsAsStrings();
            });

    }

在configurre方法中添加刚刚

          app.UseSwagger();
          app.UseSwaggerUi();   // inside this method we can pass the root path of swagger

它正在使用 localhost 但不在虚拟目录中。所以我不得不将以下配置添加到 web.config 然后它开始工作。

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

【讨论】:

  • 我不能这样做,因为我正在为 ASP.NET Core RC1 webapi 项目使用 swagger 集成。在 RC1 中,找不到 index.html。
  • 与@JAK 相同的问题。它是虚拟的
【解决方案2】:

Swager 从存储在 xml 文件中的代码 cmets 生成 API 文档。例如,GatewayServer.dll 的 cmets 将存储在 GatewayServer.xml 中。您需要将其上传到部署服务器上。

【讨论】:

    猜你喜欢
    • 2017-05-16
    • 1970-01-01
    • 2017-03-28
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2018-05-22
    相关资源
    最近更新 更多