ASP.NET核心:根据需要标记[FromQuery]参数
ASP.NET Core: Mark [FromQuery] Parameter as required
在我的控制器中有一种方法接受一个路由和一个查询参数作为参数:
/// <summary>
/// My Method
/// </summary>
/// <param name="routeParameter">Nice description of route parameter.</param>
/// <param name="queryParameter">Nice description of query paramter.</param>
[HttpPost("somePath/{routeParameter}")]
public IActionResult MyMethod([FromRoute] string routeParameter, [FromQuery] DateTime queryParamter)
{
// do something
}
在通过 Swashbuckle XML 从此签名生成的 OpenApi.json / Swagger 中,routeParameter
(路径)始终是必需的,但 queryParameter
(查询)被标记为可选。
如何将查询参数也标记为必需的?
这是 [FromUri]
接收参数的替代方法
[HttpPost("somePath")]
public IActionResult MyMethod([FromUri] Paging paging, [FromUri] QueryParam param)
{
// do something
}
QueryParam.cs
public class QueryParam
{
[Required]
public string routeParameter{ get; set; }
}
招摇UI
How can I mark the query parameter as required, too?
只需使用 [FromQuery, BindRequired] 如下:
[HttpPost("somePath/{routeParameter}")]
public IActionResult MyMethod([FromRoute] string routeParameter, [FromQuery, BindRequired] DateTime queryParamter)
{
// do something
}
测试结果如下:
在我的控制器中有一种方法接受一个路由和一个查询参数作为参数:
/// <summary>
/// My Method
/// </summary>
/// <param name="routeParameter">Nice description of route parameter.</param>
/// <param name="queryParameter">Nice description of query paramter.</param>
[HttpPost("somePath/{routeParameter}")]
public IActionResult MyMethod([FromRoute] string routeParameter, [FromQuery] DateTime queryParamter)
{
// do something
}
在通过 Swashbuckle XML 从此签名生成的 OpenApi.json / Swagger 中,routeParameter
(路径)始终是必需的,但 queryParameter
(查询)被标记为可选。
如何将查询参数也标记为必需的?
这是 [FromUri]
接收参数的替代方法
[HttpPost("somePath")]
public IActionResult MyMethod([FromUri] Paging paging, [FromUri] QueryParam param)
{
// do something
}
QueryParam.cs
public class QueryParam
{
[Required]
public string routeParameter{ get; set; }
}
招摇UI
How can I mark the query parameter as required, too?
只需使用 [FromQuery, BindRequired] 如下:
[HttpPost("somePath/{routeParameter}")]
public IActionResult MyMethod([FromRoute] string routeParameter, [FromQuery, BindRequired] DateTime queryParamter)
{
// do something
}
测试结果如下: