加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ASP.NET Core RC1 – WebAPI Swagger集成 – “错误”Sche

发布时间:2020-12-15 08:00:40 所属栏目:百科 来源:网络整理
导读:我已经在我的ASP.NET Core RC1应用程序中使用以下NuGet包集成了swagger. "Swashbuckle.SwaggerGen": "6.0.0-rc1-final","Swashbuckle.SwaggerUi": "6.0.0-rc1-final" 这是swagger集成的代码. public void ConfigureServices(IServiceCollection services) {
我已经在我的ASP.NET Core RC1应用程序中使用以下NuGet包集成了swagger.
"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();
    }

该代码使用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"}]}.

解决方法

我认为它试图从错误的路径访问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();
            });

    }

在configurre mehod添加了

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读