您还可以使用SwaggerOperationAttribute:
public class UserController : ApiController { [Route("api/user")] [SwaggerOperation(Tags = new[] { "User" })] IHttpActionResult GetUser() { ... } } public class ResumeController : ApiController { [Route("api/user/resumes")] [SwaggerOperation(Tags = new[] { "User" })] IHttpActionResult GetResumes() { ... } }
有一种方法 - 尽管没有魔术属性 - 您可以更改swagger启动配置中的默认分组规则,以便引入您自己的自定义属性.
GlobalConfiguration.Configuration .EnableSwagger(c => { c.GroupActionsBy(apiDesc => apiDesc .GetControllerAndActionAttributes().Any() ? apiDesc.GetControllerAndActionAttributes /// Forces method to be displayed within specified group, regardless of controller /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class MethodGroupAttribute : Attribute { /// /// Group name /// public string GroupName { get; private set; } ////// ctor /// /// public MethodGroupAttribute(string groupName) { if (string.IsNullOrEmpty(groupName)) { throw new ArgumentNullException("groupName"); } GroupName = groupName; } }
用法:
[Route("api/user")] [MethodGroup("User")] IHttpActionResult GetUser() { ... }