【发布时间】:2018-07-10 23:29:13
【问题描述】:
我正在为我的 WebAPI 项目使用 Swagger 和 SwaggerUi(通过 Swashbuckle.AspNetCore Nuget 包)。
我遇到的问题是 UI 没有正确形成 Try It Out 功能的请求。例如,我的 API 在服务器的方法签名中有一个可以为空的long。该参数称为modifiedSince。
当我点击执行时,modifiedSince文本输入中没有值,点击以下Urlhttps://localhost:44348/v1/Complication/%7BmodifiedSince%7D
它正在拾取占位符文本并将其包含在 URL 中(在 html 编码的花括号内)。
在另一个端点上,我有两个参数,URL 看起来像这样:https://localhost:44348/v1/Logbook/37193/Entry/2121321
但是当我点击 Try It Out 按钮时,SwaggerUi 会发送https://localhost:44348/v1/Logbook/37193/Entry/%7BentryId%7D?entryId=2121321,结果,我的服务器方法中的 entryId 参数会出现一个空值。
同样,相关文本输入的占位符被添加到 url。
我从 2.5.0 升级到 3.0.0 以解决这个问题。但它仍在发生。
Swagger API 正在执行的控制器操作的示例是:
/// <summary>
/// Logbooks of user.
/// </summary>
/// <param name="modifiedSince"></param>
/// <returns></returns>
/// <response code="200">Logbooks assigned to the user</response>
/// <response code="204">No logbooks assigned to the user</response>
/// <response code="400">Error state. Error message included in payload.</response>
/// <response code="403">Authentication required.</response>
[HttpGet]
[Route("[action]/{modifiedSince?}")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(ApiResponse<IEnumerable<LogbookDto>>), 200)]
[ProducesResponseType(204)]
[ProducesResponseType(typeof(ApiResponse<string>), 400)]
[ProducesResponseType(typeof(ApiResponse<string>), 403)]
public async Task<IActionResult> Logbook(long? modifiedSince)
{
var logbookDtos = default(IEnumerable<LogbookDto>);
if (await AuthorizeAsync(User, IdentityConstants.PolicyNames.RacsUser) &&
await Execute(async () => logbookDtos = await _mediator.Send(
new GetLogbooksQuery
{
UserId = _currentUser.UserId
//LastConfigChange = NULL
}))
)
{
if (logbookDtos.Any())
return Ok(new ApiResponse<IEnumerable<LogbookDto>> { Data = logbookDtos, Outcome = new OperationOutcome { Error = false, Message = "" } });
return NoContent();
}
return ErrorResponse(new ApiResponse<string> { Data = Errors, Outcome = new OperationOutcome { Error = true, Message = "" } });
}
这是来自 Chrome 开发工具的图片,用于描述正在发生的事情:
有人知道发生了什么吗?我是否错过了某个地方的配置?
【问题讨论】:
-
请分享您遇到问题的控制器的代码。
-
您是否测试过从参数中删除可空值?
-
@HelderSepu 抱歉耽搁了。我在另一个时区。我从 Action 方法的参数中删除了 nullable,并且正确地形成了 url
https://localhost:44348/v1/Logbook。我现在将使用控制器代码更新问题。我希望删除参数只是一种故障排除技术,因为它并不能真正解决问题。
标签: swagger swagger-ui swashbuckle