WebAPI2 可选参数无法正常工作
WebAPI2 Optional Parameters not working correctly
我正在尝试为我的 WebAPI2 服务函数设置可选参数。
这是路线的定义:
[HttpGet, Route("mp/CEG/{ST?}/{PR?}/{DES?}"), ResponseType(typeof(CEG))]
public object CEG(string ST = null, string PR = null, string DES = null)
但是,当我如下记录它并尝试 运行 在 Swagger UI(使用 Swashbuckle 构建)中进行测试时,
所有参数都声明它们是必需的,我无法提交没有参数的测试。
XML文档:
/// <summary>
/// Gets list of Apps for passed ST, PR and/or DES
/// </summary>
/// <param name="ST">ST</param>
/// <param name="PR">PR</param>
/// <param name="DES">DES</param>
/// <returns>List of CE Approvals</returns>
有什么想法吗?
当 运行 在 Visual Studio 中进行功能测试时,该函数可以在没有参数的情况下工作。
只是 SwaggerUI/Swashbuckle 导致了这个问题。我用它来让我的消费者有机会进行测试。
路线更新
[HttpGet, Route("mp/CEG/{ST}"),
Route("mp/CEG/{PR}"),
Route("mp/CEG/{DES}"),
Route("mp/CEG/{ST}/{PR}"),
Route("mp/CEG/{ST}/{DES}"),
Route("mp/CEG/{PR}/{DES}"),
ResponseType(typeof(CEG))]
您的路由不符合 Swagger 2.0。正如 specification 所说的参数(用我的话):
If the parameter is in "path", the parameter is always required.
为什么不将参数更改为查询参数,如下所示:
[HttpGet, Route("mp/CEG"), ResponseType(typeof(CEG))]
public object CEG(string ST = null, string PR = null, string DES = null)
我正在尝试为我的 WebAPI2 服务函数设置可选参数。 这是路线的定义:
[HttpGet, Route("mp/CEG/{ST?}/{PR?}/{DES?}"), ResponseType(typeof(CEG))]
public object CEG(string ST = null, string PR = null, string DES = null)
但是,当我如下记录它并尝试 运行 在 Swagger UI(使用 Swashbuckle 构建)中进行测试时, 所有参数都声明它们是必需的,我无法提交没有参数的测试。
XML文档:
/// <summary>
/// Gets list of Apps for passed ST, PR and/or DES
/// </summary>
/// <param name="ST">ST</param>
/// <param name="PR">PR</param>
/// <param name="DES">DES</param>
/// <returns>List of CE Approvals</returns>
有什么想法吗?
当 运行 在 Visual Studio 中进行功能测试时,该函数可以在没有参数的情况下工作。
只是 SwaggerUI/Swashbuckle 导致了这个问题。我用它来让我的消费者有机会进行测试。
路线更新
[HttpGet, Route("mp/CEG/{ST}"),
Route("mp/CEG/{PR}"),
Route("mp/CEG/{DES}"),
Route("mp/CEG/{ST}/{PR}"),
Route("mp/CEG/{ST}/{DES}"),
Route("mp/CEG/{PR}/{DES}"),
ResponseType(typeof(CEG))]
您的路由不符合 Swagger 2.0。正如 specification 所说的参数(用我的话):
If the parameter is in "path", the parameter is always required.
为什么不将参数更改为查询参数,如下所示:
[HttpGet, Route("mp/CEG"), ResponseType(typeof(CEG))]
public object CEG(string ST = null, string PR = null, string DES = null)