WEB API和Swagger

第一步:需要进行EF映射

 

 第二步:安装NuGet  Swagger.Net和Swashbuckle.Core

 

 

第三步:在库文件的属性中生成XML文件

 

 

第四部:修改App_Start中的SwaggerConfig.cs类和SwaggerNet.cs类

 

 

 

 SwaggerConfig.cs类代码

 public class SwaggerConfig
    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;

            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {
                        
                        c.SingleApiVersion("v1", "WebApplication1");
                        
                        c.IncludeXmlComments(GetXmlCommentsPath());
                
                    });
        }
        private static string GetXmlCommentsPath()
        {
            return string.Format(@"{0}\bin\WebApplication1.XML", System.AppDomain.CurrentDomain.BaseDirectory);
        }
    }

 SwaggerNet.cs类代码

public static class SwaggerNet 
    {
        public static void PreStart() 
        {
            RouteTable.Routes.MapHttpRoute(
                name: "SwaggerApi",
                routeTemplate: "api/docs/{controller}",
                defaults: new { swagger = true }
            );            
        }
        
        public static void PostStart() 
        {
            var config = GlobalConfiguration.Configuration;

            config.Filters.Add(new SwaggerActionFilter());
            
            try
            {
                config.Services.Replace(typeof(IDocumentationProvider),
                    new XmlCommentDocumentationProvider(HttpContext.Current.Server.MapPath("~/bin/WebApplication1.XML")));
            }
            catch (FileNotFoundException)
            {
                throw new Exception("Please enable \"XML documentation file\" in project properties with default (bin\\WebApplication1.XML) value or edit value in App_Start\\SwaggerNet.cs");
            }
        }
    }

 第五步:在Controllers文件夹中新建Controller控制器在里面写入需要的方法

这是我自己建立的record_s控制器然后再控制器里写入你需要的方法就行了比如这样

        [HttpGet]//使用Get方法
        [Route("SelectSelfLevelRank")]//路由
        public int SelectSelfLevelRank(int user_id)
        {
            int a = 0;
            if (user_id==1)
            {
                a = 1;
            }
            return a;
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值