[HttpGet]
[Route("students")]
[SwaggerOperation(Tags = new[] { "Student" })]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(ResponseModel<IList<Student>>))]
[SwaggerResponseExample(HttpStatusCode.OK, typeof(StudentResponseExample))]
[SwaggerResponse(HttpStatusCode.InternalServerError)]
public IHttpActionResult SearchStudent()
{
IDictionary<string, string> searchParams = null;
searchParams = ControllerContext.GetQueryStrings();
.
.
.
}
上述 API 有三个可选参数,它们将作为查询字符串传递
- SyncDate - 长
- 偏移量 - int
- 限制 - 整数
用户无法在 swagger UI 中输入这些可选查询参数。请指导我实现可选的查询参数。
我正在使用 swashbuckle,并且我更喜欢使用注释,而不是在每个 API 方法上使用冗长的注释部分来实现 swagger 功能。
我提到了以下内容将查询字符串参数添加到我的 Swagger 规范中 https://stackoverflow.com/questions/35828328/adding-query-string-params-to-my-swagger-specs并创建了Swagger参数属性上课于FiltersWeb API 的文件夹以及尝试添加时操作过滤器 in G全局配置.配置
.EnableSwagger正如给定的,它抛出类型或命名空间名称Swagger参数属性处理程序找不到。我什至添加了Filters文件夹命名空间,但错误仍然存在。
请指导如何在 swagger 中实现可选查询参数
Swagger 的工作方式是根据您的 Action 签名提取参数,即您的 Action 的参数,但在这里您是从 ControllerContext 获取这些值,这显然 Swagger 永远不会意识到。
因此,您需要更改操作的签名并将参数传递到那里。
如果您将它们设置为可为空类型,它们将被视为可选 -
[HttpGet]
[Route("students")]
[SwaggerOperation(Tags = new[] { "Student" })]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(ResponseModel<IList<Student>>))]
[SwaggerResponseExample(HttpStatusCode.OK, typeof(StudentResponseExample))]
[SwaggerResponse(HttpStatusCode.InternalServerError)]
public IHttpActionResult SearchStudent(long? SyncDate = null,int? OffSet = null,int? Limit = null)
{
// Use the variables here
.
.
.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)