ASP.NET Core RC1 - WebAPI Swagger集成 - "错误"SchemaValidationMessages

JAK*_*JAK 7 c# swagger asp.net-core

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

"Swashbuckle.SwaggerGen": "6.0.0-rc1-final",
"Swashbuckle.SwaggerUi": "6.0.0-rc1-final"
Run Code Online (Sandbox Code Playgroud)

这是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();
    }
Run Code Online (Sandbox Code Playgroud)

该代码使用localhost在本地访问时生成swagger文档 - >" 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"}]}.
Run Code Online (Sandbox Code Playgroud)

Ven*_*nky 5

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

 $(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
        }
}
Run Code Online (Sandbox Code Playgroud)

更新.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();
            });

    }
Run Code Online (Sandbox Code Playgroud)

在configurre mehod添加了

          app.UseSwagger();
          app.UseSwaggerUi();   // inside this method we can pass the root path of swagger
Run Code Online (Sandbox Code Playgroud)

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

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
Run Code Online (Sandbox Code Playgroud)


小智 1

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