From 32bc0fe1d06c151408b49eef83e4e3f22d78aa06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?jian=E7=8E=84=E5=86=B0?= Date: Fri, 15 Jun 2018 15:51:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=8A=E4=BC=A0=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../App_Start/SwaggerConfig.cs | 23 +++++++++-- .../Admin/Controllers/TestAController.cs | 41 ++++++++++++++++++- .../Extensions/AddUploadOperationFilter.cs | 38 +++++++++++++++++ .../Extensions/UploadAttribute.cs | 25 +++++++++++ SwashbuckleEx.WebApiTest/Models/UserInfo.cs | 33 +++++++++++++++ .../SwashbuckleEx.WebApiTest.csproj | 5 ++- 6 files changed, 158 insertions(+), 7 deletions(-) create mode 100644 SwashbuckleEx.WebApiTest/Extensions/AddUploadOperationFilter.cs create mode 100644 SwashbuckleEx.WebApiTest/Extensions/UploadAttribute.cs create mode 100644 SwashbuckleEx.WebApiTest/Models/UserInfo.cs diff --git a/SwashbuckleEx.WebApiTest/App_Start/SwaggerConfig.cs b/SwashbuckleEx.WebApiTest/App_Start/SwaggerConfig.cs index 3c46cbe..f4d050e 100644 --- a/SwashbuckleEx.WebApiTest/App_Start/SwaggerConfig.cs +++ b/SwashbuckleEx.WebApiTest/App_Start/SwaggerConfig.cs @@ -6,6 +6,7 @@ using System.Web.Http.Description; using Swashbuckle.Application; using SwashbuckleEx.WebApiTest; +using SwashbuckleEx.WebApiTest.Extensions; using SwashbuckleEx.WebApiTest.Selectors; [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] @@ -20,7 +21,17 @@ public static void Register() GlobalConfiguration.Configuration .EnableSwagger(c => { - //c.SingleApiVersion("v1", "Test.WebApi"); + // 配置简单API文档信息-用于单个文档 + //c.SingleApiVersion("v1", "Test.WebApi").Contact(x => + //{ + // x.Email("jianxuanhuo1@126.com"); + // x.Name("jian玄冰"); + // x.Url("https://www.cnblogs.com/jianxuanbing"); + //}).TermsOfService("jian玄冰1").License(x => + //{ + // x.Name("MIT"); + // x.Url("https://www.cnblogs.com/jianxuanbing"); + //}).Description("自定义文案内容,可以随便输入内容"); c.MultipleApiVersions(ResolveAreasSupportByRouteConstraint, (vc) => { vc.Version("Admin", "中文后台 API").Description("这个用于测试一下备注信息").TermsOfService("www.baidu.com").License( @@ -33,10 +44,11 @@ public static void Register() { x.Name("2017").Email("jianxuanhuo1@126.com").Url("www.baidu.xxxx"); }); - vc.Version("v1", "Common API",true); - - vc.Version("Client", "Client API"); + vc.Version("v1", "Common API", true); + + vc.Version("Client", "Client API"); }); + c.ApiKey("Authorization").Description("OAuth2 Auth").In("header").Name("Authorization"); //c.OAuth2("jwt").AuthorizationUrl("http://localhost:9460/oauth/token") // .TokenUrl("http://localhost:9460/oauth/token").Scopes( @@ -45,6 +57,9 @@ public static void Register() // x.Add("scope", "admin"); // }); c.DocumentFilter(); + + c.OperationFilter(); + c.IncludeXmlComments(string.Format("{0}/bin/SwashbuckleEx.WebApiTest.XML", AppDomain.CurrentDomain.BaseDirectory)); c.ShowDeveloperInfo(); }) diff --git a/SwashbuckleEx.WebApiTest/Areas/Admin/Controllers/TestAController.cs b/SwashbuckleEx.WebApiTest/Areas/Admin/Controllers/TestAController.cs index 5d3aa7c..51b581a 100644 --- a/SwashbuckleEx.WebApiTest/Areas/Admin/Controllers/TestAController.cs +++ b/SwashbuckleEx.WebApiTest/Areas/Admin/Controllers/TestAController.cs @@ -1,6 +1,10 @@ using System; +using System.Net; +using System.Net.Http; using System.Web.Http; using Swashbuckle.Swagger.Annotations; +using SwashbuckleEx.WebApiTest.Extensions; +using SwashbuckleEx.WebApiTest.Models; namespace SwashbuckleEx.WebApiTest.Areas.Admin.Controllers { @@ -20,13 +24,46 @@ public TestAController() /// /// 获取后台Guid /// + /// + /// 测试一些内容,不想将无用的东西放在接口名称当中
+ /// 换行输出一下内容 + ///
/// [HttpGet] - [AllowAnonymous] - [ApiAuthor(Name = "jian玄冰",Status = DevStatus.Wait,Time = "2018-04-28")] public Guid GetGuid() { return Guid.NewGuid(); } + + /// + /// 上传文件 + /// + [HttpPost] + [Upload] + public void UploadFile() + { + + } + + /// + /// 查看API开发状态 + /// + [HttpGet] + [ApiAuthor(Name = "jian玄冰", Status = DevStatus.Wait, Time = "2018-04-28")] + public void ApiStatus() + { + + } + + /// + /// 获取用户信息 + /// + /// + [HttpGet] + [SwaggerResponse(HttpStatusCode.OK,"自定义内容",Type = typeof(UserInfo))] + public HttpResponseMessage GetUserInfo() + { + return Request.CreateResponse(HttpStatusCode.OK, new UserInfo(), "application/json"); + } } } \ No newline at end of file diff --git a/SwashbuckleEx.WebApiTest/Extensions/AddUploadOperationFilter.cs b/SwashbuckleEx.WebApiTest/Extensions/AddUploadOperationFilter.cs new file mode 100644 index 0000000..a015938 --- /dev/null +++ b/SwashbuckleEx.WebApiTest/Extensions/AddUploadOperationFilter.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; +using System.Linq; +using System.Web.Http.Description; +using Swashbuckle.Swagger; + +namespace SwashbuckleEx.WebApiTest.Extensions +{ + /// + /// 添加 上传操作过滤 + /// + public class AddUploadOperationFilter : IOperationFilter + { + /// + /// 重写Apply方法,加入Upload操作过滤 + /// + public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) + { + var upload = apiDescription.ActionDescriptor.GetCustomAttributes().FirstOrDefault(); + if (upload == null) + { + return; + } + operation.consumes.Add("application/form-data"); + if (operation.parameters==null) + { + operation.parameters = new List(); + } + operation.parameters.Add(new Parameter() + { + name = upload.Name, + @in = "formData", + required = upload.Require, + type = "file", + description = upload.Description + }); + } + } +} \ No newline at end of file diff --git a/SwashbuckleEx.WebApiTest/Extensions/UploadAttribute.cs b/SwashbuckleEx.WebApiTest/Extensions/UploadAttribute.cs new file mode 100644 index 0000000..bb92809 --- /dev/null +++ b/SwashbuckleEx.WebApiTest/Extensions/UploadAttribute.cs @@ -0,0 +1,25 @@ +using System; + +namespace SwashbuckleEx.WebApiTest.Extensions +{ + /// + /// 上传属性,用于标识接口是否包含上传信息参数 + /// + public class UploadAttribute : Attribute + { + /// + /// 参数名 + /// + public string Name { get; set; } = "file"; + + /// + /// 是否必须包含文件 + /// + public bool Require { get; set; } = true; + + /// + /// 备注 + /// + public string Description { get; set; } = ""; + } +} \ No newline at end of file diff --git a/SwashbuckleEx.WebApiTest/Models/UserInfo.cs b/SwashbuckleEx.WebApiTest/Models/UserInfo.cs new file mode 100644 index 0000000..b79cbb6 --- /dev/null +++ b/SwashbuckleEx.WebApiTest/Models/UserInfo.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace SwashbuckleEx.WebApiTest.Models +{ + /// + /// 用户信息 + /// + public class UserInfo + { + /// + /// 名称 + /// + public string Name { get; set; } + + /// + /// 手机号码 + /// + public string Phone { get; set; } + + /// + /// 头像 + /// + public string Avactor { get; set; } + + /// + /// 账号 + /// + public string Account { get; set; } + } +} \ No newline at end of file diff --git a/SwashbuckleEx.WebApiTest/SwashbuckleEx.WebApiTest.csproj b/SwashbuckleEx.WebApiTest/SwashbuckleEx.WebApiTest.csproj index 05bb836..02ccb17 100644 --- a/SwashbuckleEx.WebApiTest/SwashbuckleEx.WebApiTest.csproj +++ b/SwashbuckleEx.WebApiTest/SwashbuckleEx.WebApiTest.csproj @@ -24,6 +24,7 @@ + true @@ -141,15 +142,18 @@ + Global.asax + + @@ -163,7 +167,6 @@ -