[英]ASP.Net Web API Swashbuckle how to ignore HttpRequestMessage
I'm using Swashbuckle to generate docs for an API. 我正在使用Swashbuckle为API生成文档。 My controller methods looks like this:
我的控制器方法如下所示:
[ResponseType(typeof(CategoryCollectionModel))]
public HttpResponseMessage Get(HttpRequestMessage request, [FromUri]Paging paging)
{
var input = new CategoriesListQuery.Input { Page = paging.Page, Size = paging.Size };
var result = this.queryInvoker.Execute<CategoriesListQuery.Input, CategoriesListQuery.Result>(input);
var items = Mapper.Map<CategoryCollectionModel>(result);
return request.CreateResponse(HttpStatusCode.OK, items);
}
Swashbuckle treats HttpRequestMessage
as a parameter in the generated docs. Swashbuckle在生成的文档中将
HttpRequestMessage
视为参数。 Is there a way to configure Swashbuckle to ignore HttpRequestMessage
since it is only included in the signature for testing purposes? 有没有一种方法可以配置Swashbuckle忽略
HttpRequestMessage
因为它仅包含在签名中以进行测试?
I found a solution from " http://www.morganskinner.com/2016/02/ignoring-parameters-in-swashbuckle.html " 我从“ http://www.morganskinner.com/2016/02/ignoring-parameters-in-swashbuckle.html ”找到了解决方案
Summary : 总结:
In Swashbuckle you can plug-in operation “filters” that can be used to alter the emitted data – the filter is passed the context of the operation being emitted, and you can monkey around with the data that pops out.
在Swashbuckle中,您可以插入操作“过滤器”,该操作可用于更改发出的数据–过滤器在发出操作的上下文中传递,并且您可以浏览弹出的数据。 All I had to do then was create a filter that would look for this datatype, and remove the corresponding data from the results.
然后,我要做的就是创建一个过滤器,该过滤器将查找此数据类型,并从结果中删除相应的数据。 I ended up with this…
我结束了这个……
public class IgnoreHttpRequestMessageOperationFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry,
ApiDescription apiDescription)
{
apiDescription.ParameterDescriptions
.Where(desc => desc.ParameterDescriptor.ParameterType
== typeof(HttpRequestMessage))
.ToList()
.ForEach(param =>
{
var toRemove = operation.parameters
.SingleOrDefault(p => p.name == param.Name);
if (null != toRemove)
operation.parameters.Remove(toRemove);
});
}
}
With that class in place, I just needed to plug this in to the swagger config file as follows...
有了该类之后,我只需要按如下所示将其插入到swagger配置文件中即可...
c.OperationFilter<IgnoreHttpRequestMessageOperationFilter>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.