WEB API和Swagger

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenguanghan123/article/details/88929939

第一步:需要进行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;
        }

猜你喜欢

转载自blog.csdn.net/chenguanghan123/article/details/88929939
今日推荐