zoukankan      html  css  js  c++  java
  • C# swagger应用

    1、使用Nuget,对WebAPI项目添加swagger-net的引用

    SwaggerConfig 配置如下

    using System.Web.Http;
    using WebActivatorEx;
    using MebAPI;
    using System; 
    using System.Collections.Generic; 
    using Swagger.Net.Application;
    using Swagger.Net;
    using System.Web.Http.Description; 
    
    [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
    namespace MebAPI
    {
        /// <summary>
        /// 
        /// </summary>
        public class SwaggerConfig
        {
            public static void Register()
            {
                var thisAssembly = typeof(SwaggerConfig).Assembly;
    
                GlobalConfiguration.Configuration
                    .EnableSwagger(c =>
                    {
                        c.SingleApiVersion("v1", "WebApi接口文档");
                        c.IncludeXmlComments(GetXmlCommentsPath(thisAssembly.GetName().Name));
                        c.UseFullTypeNameInSchemaIds();
                        c.OperationFilter<HttpAuthHeaderFilter>();
    
                    })
                    .EnableSwaggerUi(c =>
                    {
    
                    });
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            protected static string GetXmlCommentsPath(string name)
            {
                return string.Format(@"{0}in{1}.XML", AppDomain.CurrentDomain.BaseDirectory, name);
            }
    
            public class HttpAuthHeaderFilter : IOperationFilter
            {
                public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
                {
                    if (operation.parameters == null)
                        operation.parameters = new List<Parameter>();
    
                    operation.parameters.Add(new Parameter { name = "Authorization", @in = "header", description = "授权", required = false, type = "header" });
    
                }
            }
        }
    
    }

    生成输出xml配置

  • 相关阅读:
    POJ1034 The dog task
    POJ1033 Defragment
    POJ1032 Parliament
    POJ1031 Fence
    POJ1030 Rating
    POJ1029 False coin
    伪元素的使用
    伪元素选择器:before 以及 :after
    jquery html() 和text()的用法
    用CSS绘制三角形
  • 原文地址:https://www.cnblogs.com/su-king/p/12167213.html
Copyright © 2011-2022 走看看