标签:mes width parameter play sem add 生成 view names
十年河东,十年河西,莫欺少年穷
学无止境,精益求精
1、新建一个webApi项目,并引用Swashbuckle,版本:5.6.0
2、引用成功后,系统会自动生成一个SwaggerConfig.cs的文件,位于App_Start文件夹内,修改这个配置文件为:
using System.Web.Http; using WebActivatorEx; using Swashbuckle.Application; using Swashbuckle.Swagger; using System.Web.Http.Description; using System.Collections.Generic; using SwaggerSite; [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] namespace SwaggerSite { public class SwaggerConfig { public static void Register() { var thisAssembly = typeof(SwaggerConfig).Assembly; GlobalConfiguration.Configuration .EnableSwagger(c => { c.SingleApiVersion("v1", "SwaggerSite"); c.IncludeXmlComments(string.Format("{0}/bin/SwaggerSite.XML", System.AppDomain.CurrentDomain.BaseDirectory)); c.OperationFilter<HttpAuthHeaderFilter>(); }) .EnableSwaggerUi(c => { }); } } 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" }); } } }
3、右键项目,选择属性-生成一栏,输出路径,XML文档文件为:bin\SwaggerSite.xml
4、修改Api控制器为:
/// <summary> /// 测试接口 /// </summary> [RoutePrefix("api/Values")] public class ValuesController : ApiController { /// <summary> /// GET api/values /// </summary> /// <returns></returns> public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } /// <summary> /// GET api/values/5 /// </summary> /// <param name="id"></param> /// <returns></returns> public string Get(int id) { return "value"; } /// <summary> /// POST api/values /// </summary> /// <param name="value"></param> public void Post([FromBody] string value) { } /// <summary> /// PUT api/values/5 /// </summary> /// <param name="id"></param> /// <param name="value"></param> public void Put(int id, [FromBody] string value) { } /// <summary> /// DELETE api/values/5 /// </summary> /// <param name="id"></param> public void Delete(int id) { } }
运行项目:https://localhost:44372/swagger
安全性方面,生成的swagger可以使用授权,关于授权,C#这块可以使用JWT+Token验证。
关于JWT授权,可以参考大牛博客:JSON WEB TOKEN,简单谈谈TOKEN的使用及在C#中的实现
纯属记录下,用到时,迅速搭建,万事大吉。
@天才卧龙的博客
,NetFamework4.5.2+搭建带有Token验证的Swagger
标签:mes width parameter play sem add 生成 view names
原文地址:https://www.cnblogs.com/chenwolong/p/Swashbuckle.html