Put Action定义为int参数类型,并且我需要在发送字符串时响应自定义BadRequest消息,该响应应该仅适用于一个Controller
[HttpPut("address")]
[Authorize]
[ProducesResponseType(typeof(Customer), 200)]
[ProducesResponseType(typeof(Error), 400)]
[ProducesResponseType(typeof(Error), 404)] //custom error
[ProducesDefaultResponseType]
public async Task<IActionResult> UpdateAddress(
[Required] [FromForm] string email,
[Required] [FromForm] string address_1,
[FromForm] string address_2,
[Required] [FromForm] string city,
[Required] [FromForm] string region,
[Required] [FromForm] string postal_code,
[Required] [FromForm] string country,
[Required] [FromForm] int shipping_region_id //this is the parameter to validate
)
{
//...
}
BadRequest(400)消息应为:“运送地区ID不是数字” 如何在到达Controller并发送自定义BadRequest对象之前验证类型?
答案 0 :(得分:2)
使用DTO进行PUT请求并像这样传递您的参数。
public class UpdateAddressDTO
{
[Required]
public string email { get; set; }
[Required]
public string address_1 { get; set; }
[Required]
public string address_2 { get; set; }
[Required]
public string city { get; set; }
[Required]
public string region { get; set; }
[Required]
public string postal_code { get; set; }
[Required]
public string country { get; set; }
}
[Route("address/{shipping_region_id}/update")]
public async Task<IActionResult> UpdateAddress([FromUri]int shipping_region_id, [FromBody]UpdateAddressDTO model)
{
}