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已经可以使用了

  • 相关阅读:
    HarmonyOS (鸿蒙操作系统)你值得拥有
    远端FTP文件与本地文件如何进行Diff
    多线程下载一个大文件的速度更快的真正原因是什么?
    一道有意思的面试题
    BootstrapVue 安装指南
    bash shell数组使用总结
    APP测试之ADB最全指南
    APP测试用例整理
    直播类音视频测试整理
    IOS手机耗电量测试
  • 原文地址:https://www.cnblogs.com/yan0720/p/11049915.html
Copyright © 2011-2022 走看看