1

I have a ASP.NET Core 7 Web API project with one action method. This action method accepts a model from request body like this:

public class ActionModel
{
    [Required]
    [MaxLength(3)]
    [RegularExpression("[a-zA-Z]+[a-zA-Z]*")]
    public string Name { get; set; }
    public Guid ProfileId { get; set; }
}

Assume that this is my action method:

[HttpPost("extra-data")]
public async Task<ActionResult> MyAction([FromBody] ActionModel requestDto)
{
    if (ModelState.IsValid)
    {
        _logger.LogError("Try to do some thing");
    }

    _logger.LogError($"{ModelState.ErrorCount}");
    return Ok();
}

I expect that the execution goes through the action method body, but the request is validated by some other middleware before reaching the action method. How can I disable this default auto-validation middleware?

2

1 Answer 1

0

I Used below code for disabling Model validations in my WebAPI Project

At Entity Level -

 [ValidateNever]
 public class Usermodel
 {
     public string Username { get; set; }
     
     public string Password { get; set; }
     
     public string EmailAddress { get; set; }
     
     public string Role { get; set; }
     
     public string UserId { get; set; }
     
     public DateTime DateofJoining { get; set; }
     
     public string Status { get; set; }
 }

At Single Property Level -

 public class Usermodel
 {
     [Required]
     public string Username { get; set; }

     [Required]
     public string Password { get; set; }

     [ValidateNever]
     public string EmailAddress { get; set; }

     [ValidateNever]
     public string Role { get; set; }

     [ValidateNever]
     public string UserId { get; set; }

     [ValidateNever]
     public DateTime DateofJoining { get; set; }

     [ValidateNever]
     public string Status { get; set; }
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.