private ResourceListing getDocs(HttpActionContext actionContext) { var assemblyType = (actionContext.ActionDescriptor as ReflectedHttpActionDescriptor).MethodInfo.DeclaringType; var docProvider = new XmlCommentDocumentationProvider(); //(XmlCommentDocumentationProvider)GlobalConfiguration.Configuration.Services.GetDocumentationProvider(); ResourceListing r = SwaggerGen.CreateResourceListing(actionContext); foreach (var api in GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions) { if (api.ActionDescriptor.ActionName.EndsWith("API"))//Ignore each Default API action { continue; } string apiControllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName; if (api.Route.Defaults.ContainsKey(SwaggerGen.SWAGGER) || apiControllerName.ToUpper().Equals(SwaggerGen.SWAGGER.ToUpper())) { continue; } // Make sure we only report the current controller docs if (!apiControllerName.Equals(actionContext.ControllerContext.ControllerDescriptor.ControllerName)) { continue; } ResourceApi rApi = SwaggerGen.CreateResourceApi(api); r.apis.Add(rApi); ResourceApiOperation rApiOperation = SwaggerGen.CreateResourceApiOperation(r, api, docProvider); rApi.operations.Add(rApiOperation); foreach (var param in api.ParameterDescriptions) { ResourceApiOperationParameter parameter = SwaggerGen.CreateResourceApiOperationParameter(r, api, param, docProvider); rApiOperation.parameters.Add(parameter); } if (System.Configuration.ConfigurationManager.AppSettings["swagger:APITOKEN"] != null && System.Configuration.ConfigurationManager.AppSettings["swagger:APITOKEN"].Equals("true") && !api.ActionDescriptor.ActionName.EndsWith("API")) { //添加Token ResourceApiOperationParameter p = new ResourceApiOperationParameter(); p.name = "ApiToken"; p.description = "Api Token"; p.paramType = "path"; p.required = true; p.dataType = "String"; rApiOperation.parameters.Insert(0, p); } SwaggerGen.CreateModel(r, api, docProvider); //r.models = new ResourceApiModel(); } return(r); }
/// <summary> /// Creates an api operation /// </summary> /// <param name="api">Description of the api via the ApiExplorer</param> /// <param name="docProvider">Access to the XML docs written in code</param> /// <returns>An api operation</returns> public static ResourceApiOperation CreateResourceApiOperation(ResourceListing r, ApiDescription api, XmlCommentDocumentationProvider docProvider) { ResourceApiOperation rApiOperation = new ResourceApiOperation() { httpMethod = api.HttpMethod.ToString(), nickname = docProvider.GetNickname(api.ActionDescriptor), responseClass = docProvider.GetResponseClass(api.ActionDescriptor), summary = docProvider.GetDocumentation(api.ActionDescriptor), notes = docProvider.GetNotes(api.ActionDescriptor), parameters = new List <ResourceApiOperationParameter>(), }; if (string.IsNullOrEmpty(rApiOperation.notes) || rApiOperation.notes.Equals("No Documentation Found.")) { rApiOperation.notes = rApiOperation.summary; } return(rApiOperation); }