ASP.Net Web API Swashbuckle如何忽略HttpRequestMessage

ASP.Net Web API Swashbuckle how to ignore HttpRequestMessage

我正在使用 Swashbuckle 为 API 生成文档。我的控制器方法如下所示:

[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 将 HttpRequestMessage 视为生成文档中的参数。有没有办法将 Swashbuckle 配置为忽略 HttpRequestMessage 因为它仅包含在签名中用于测试目的?

请参考讨论。简而言之,不要像输入参数那样传入 HttpRequestMessage,而是模拟 {controller}.Request 属性.

我从“http://www.morganskinner.com/2016/02/ignoring-parameters-in-swashbuckle.html

找到了解决方案

总结:

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. 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...

c.OperationFilter<IgnoreHttpRequestMessageOperationFilter>();

为我工作。谢谢“Morgan