ASP.NET Core RC1 - WebAPI Swagger Integration - "Error&

本文关键字:quot Error Integration WebAPI NET Core RC1 ASP Swagger | 更新日期: 2023-09-27 17:52:42

我已经在我的ASP中集成了swagger。. NET Core RC1应用程序,使用以下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"}]}.

ASP.NET Core RC1 - WebAPI Swagger Integration - "Error&

我认为它试图从错误的路径访问json数据。确保在swaggerui

testapiproject/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();
            });
    }

在配置方法中添加了

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

它在本地主机上工作,但不在虚拟目录中。所以我不得不添加以下配置到web。

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

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