【问题标题】:Error when calling UseSwagger in Azure Web API在 Azure Web API 中调用 UseSwagger 时出错
【发布时间】:2017-06-20 19:57:58
【问题描述】:

我创建了 VS2015 和 .Net Core Web API 项目。 我正在关注http://www.technicalblogs.sentientmindz.com/2017/04/09/enabling-swagger-support-to-the-web-api/中的示例

我已经安装了 Swashbuckle.AspNetCore,接下来尝试编写代码,但在使用 UseSwagger 时出现错误。请给我建议。

/* Startup.cs */
using System;  
using System.Collections.Generic;  
using System.Linq;  
using Microsoft.Owin;  
using Owin;  
using Swashbuckle.AspNetCore.Swagger;  
using Microsoft.Extensions.DependencyInjection;  

[assembly: OwinStartup(typeof(TestApi.Startup))]

namespace TestApi
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
        ConfigureAuth(app);

        /*use swagger added by me*/
        app.UseSwagger();      /*ERROR:iAppBuilder does not contain definition for UseSwagger…*/
        app.UseSwaggerUI(c =>. /*ERROR :iAppBuilder does not contain definition for UseSwaggerUI…*/
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Accounts API V1");  
        });  

    }

    //Add framework services by me
    public void ConfigureServices(IServiceCollection services) {
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "AcccountsAPI", Version = "v1" });  
        });  

        }
    }
}

【问题讨论】:

    标签: asp.net-core asp.net-web-api2 swagger-ui


    【解决方案1】:

    我从您的代码中假设您使用的是 .Net Core v1.1,这就是我的做法:

        public void Configuration(IAppBuilder app)
        {
            HttpConfiguration config = new HttpConfiguration();
            WebApiConfig.Register(config);
            config.EnableSwagger(c =>
            {
                c.SingleApiVersion("v1", "WebAPI");
                c.IncludeXmlComments(GetXmlCommentsPath());
                c.ResolveConflictingActions(x => x.First());
    
            }).EnableSwaggerUi();
        }
    
        protected static string GetXmlCommentsPath()
        {
            return System.String.Format($@"{0}\bin\MyApi.XML",
                    System.AppDomain.CurrentDomain.BaseDirectory);
        }
    

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      相关资源
      最近更新 更多