-5

a Put Action is defined with a parameter type int and I need to respond a custom BadRequest message when a string is sent instead, this response should be only for one 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
        )
        {
           //...
        }

the BadRequest (400) message should be : "The Shipping Region ID is not number" How I can validate the type before to reach the Controller and send a custom BadRequest object?

alvan
  • 11
  • 2

1 Answers1

2

Use DTO for PUT request and pass your parameter like this.

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)
{

}
awais
  • 492
  • 4
  • 17