Created
August 6, 2015 04:11
-
-
Save caevyn/767413b39cc5760e7731 to your computer and use it in GitHub Desktop.
web api description filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ApiOperationDescriptionAttribute : Attribute | |
{ | |
public string Description { get; set; } | |
public ApiOperationDescriptionAttribute(string description) | |
{ | |
Description = description; | |
} | |
} | |
public class LookupOperationFilter : IOperationFilter | |
{ | |
public void Apply(Operation operation, SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription) | |
{ | |
var attribs = apiDescription.ActionDescriptor.GetCustomAttributes<ApiOperationDescriptionAttribute>(); | |
var desc = attribs.FirstOrDefault(); | |
if (desc != null) | |
{ | |
operation.description = desc.Description; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
var config = new HttpConfiguration(); | |
config.EnableSwagger(c => | |
{ | |
c.SingleApiVersion("v1", "My API"); | |
c.DescribeAllEnumsAsStrings(); | |
c.OperationFilter<LookupOperationFilter>(); | |
}); | |
WebApiConfig.Register(config); | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment