zoukankan      html  css  js  c++  java
  • 使用Swagger创建Api

    1.首先创建一个web项目,选择Mvc模板

    2.右键点击引用.管理Nuget程序包,浏览 搜索Swagger,下载安装下面的包

    3.安装完后在App_Start里面会出现SwaggerConfig.cs类,并将SwaggerConfig类中的内容替换成下内容

    using System.Web.Http;
    using WebActivatorEx;
    using UseSwagger;
    using Swashbuckle.Application;
    using System;
    
    [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
    
    namespace UseSwagger
    {
        public class SwaggerConfig
        {
            public static void Register()
            {
                var thisAssembly = typeof(SwaggerConfig).Assembly;
    
                GlobalConfiguration.Configuration
                    .EnableSwagger(c =>
                    {
                        c.SingleApiVersion("v1", "WebApp");
    
    
                    })
                    .EnableSwaggerUi(c =>
                    {
                        GetXmlCommentsPath();
                    });
    
            }
            private static string GetXmlCommentsPath()
            {
                return string.Format(@"{0}inUseSwagger.XML", AppDomain.CurrentDomain.BaseDirectory);
            }
        }
    }

    4.在App_Start文件夹中创建一个WebApiConfig.cs内容为

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    
    namespace UseSwagger.App_Start
    {
        public class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API 配置和服务
    
                // Web API 路由
                config.MapHttpAttributeRoutes();
                
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new
                    {
                        id = RouteParameter.Optional
                    }
                );
            }
        }
    }

    此时会发现 config.MapHttpAttributeRoutes();飘红报错.此时需在引用  Microsoft.AspNet.WebApi.WebHost  包.然后就不报错了.

    5.在Global.asax调用刚才添加的类的Register方法.

    6.右键项目->属性->生成->勾上XML文档文件

    然后继续点击Web,设置默认打开页面(此处若不设置默认打开页面.运行项目将会报错404,因为项目运行之后的地址不对.正确的地址是 项目地址/swagger/ui/index)

    7.创建一个Controller,然后继承ApiController,记得添加引用 using System.Web.Http;(若不继承自ApiController则不会再Swagger页面中显示)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Mvc;
    
    namespace UseSwagger.Controllers
    {
        public class TestController : ApiController
        {
            // GET: Test
            public int Index(int a)
            {
                return 0;
            }
        }
    }

    至此Swagger已经可以使用了

  • 相关阅读:
    C语言宏定义##连接符和#符的使用
    C语言宏高级用法 [总结]
    101平衡模式 DIR的理解
    MACHINE_START 怎样调用
    SYSCALL_DEFINE3宏定义的分析
    ARM-Linux系统调用流程
    SYSCALL_DEFINE3 宏定义的转换
    socket编程之select()
    socket编程之select()
    Oracle 常用的查询操作
  • 原文地址:https://www.cnblogs.com/yan0720/p/11049915.html
Copyright © 2011-2022 走看看