简体   繁体   English

ASP.Net Web API Swashbuckle如何忽略HttpRequestMessage

[英]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因为它仅包含在签名中以进行测试?

Please refer to the discussion here . 请参考这里的讨论。 In short do not pass in HttpRequestMessage as in input parameter, rather mock the {controller}.Request property. 简而言之,不要像输入参数那样传递HttpRequestMessage ,而要模拟{controller}.Request属性。

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>();

Working for me. 为我工作。 thanks " Morgan " 谢谢“ 摩根

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 逐步在ASP.Net Web API上运行swashbuckle - step by step to run swashbuckle on ASP.Net Web API 使用 ASP.NET Core Web API 重命名 Swashbuckle 6 (Swagger) 中的模型 - Rename model in Swashbuckle 6 (Swagger) with ASP.NET Core Web API ASP.Net核心API访问HttpRequestMessage - ASP.Net Core API Accessing HttpRequestMessage 忽略 ASP.NET Web ZDB974238714CA8DE634A7CE1D083A14 中的 controller - Ignore controller in ASP.NET Web API 如何忽略 ASP.NET 5 Web API 中的网站图标调用 - How to ignore favicon call in ASP.NET 5 Web API 在ASP.Net Web API中的日志记录DelegatingHandler中读取HttpRequestMessage.Content时丢失 - HttpRequestMessage.Content is lost when it is read in a logging DelegatingHandler in ASP.Net Web API 无法发布 ASP.NET Web API 到 Azure 应用服务(带 SwashBuckle) - Cannot publish ASP.NET Web API to Azure App Service (with SwashBuckle) ASP.NET WEB API-CamelCasePropertyNamesContractResolver-强制忽略特定端点 - ASP.NET WEB API - CamelCasePropertyNamesContractResolver - force to ignore specific endpoints Swashbuckle/Swagger + ASP.Net Core:“无法加载 API 定义” - Swashbuckle/Swagger + ASP.Net Core: "Failed to load API definition" 与HttpRequestMessage等效的ASP.NET Core是什么? - What is the ASP.NET Core equivalent to HttpRequestMessage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM