How to disable server side validation mvc web api controller

mvc validate model in controller
asp.net core web api model validation
asp.net core client side validation not working
asp.net core custom validation attribute
mvc custom validation
asp.net core web api custom model validation
mvc ignore validation property
mvc client side validation

How to disable server side validation mvc web api controller. please tell me a simple way to use my custom validation.

The answer from @peco only clears the validation, but the validation runs anyway.

To disable the validation for a controller you can clear the ModelValidatorProvider for the specific controller with a custom IControllerConfiguration attribute.

public class DisableModelValidatorAttribute : Attribute, IControllerConfiguration
{
    public void Initialize(HttpControllerSettings settings,
        HttpControllerDescriptor descriptor)
    {
        settings.Services.Clear(typeof(ModelValidatorProvider));
    } 
}

And just apply the Attribute to the controller:

[DisableModelValidator]
public class SomeController : ApiController
{
    public IHttpActionResult Post(MyDto dto)
    {
        // ModelState.IsValid is always true now
        return Ok();
    }
}

see also: https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/configuring-aspnet-web-api and disable default validation in Asp.Net WebAPI

Disable Automatic Model State Validation in ASP.NET Core 2.1, When the controller is decorated with APIController attribute, the framework would automatically register a ModelStateInvalidFilter which runs on  MVC3 & MVC4 supports unobtrusive client-side validation. In which validation rules are defined using attributes added to the generated HTML elements. These rules are interpreted by the included JavaScript library and uses the attribute values to configure the jQuery Validation library which does the actual validation work.

try to use

[ValidateInput(false)]

with action method

How to Enable and Disable Client-Side Validation in MVC, MVC3 & MVC4 supports unobtrusive client-side validation. SQL Server · MongoDB How to Enable and Disable Client-Side Validation in MVC controller MVC design pattern which helps to create dynamic Web apps and single page ASP.NET Web API Questions and Answers. This book is designed​  Disable Automatic Model State Validation in ASP.NET Core 2.1. You can remove the APIController attribute to disable the automatic model validation. But, then you will lose the other benefits of this attribute like disabling conventional routing and allowing model binding without adding [FromBody] parameter attributes. The better approach to disable the default behavior by setting SuppressModelStateInvalidFilter option to true. You can set this option to true in the ConfigureServices method

There is no ValidateInput attribute for web api that will disable validation, but you can easily define one that will reset the ModelState:

public class ValidateInput : ActionFilterAttribute
{
    private readonly bool _enableValidation;

    public ValidateInput(bool enableValidation)
    {
        _enableValidation = enableValidation;
    }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if(_enableValidation)
        {
            return;
        }

        if (!actionContext.ModelState.IsValid)
        {
            actionContext.ModelState.Clear();
        }
    }
}

And then use this in your controller:

public class SomeController : ApiController
{
    [ValidateInput(false)]
    public IHttpActionResult Post(MyDto dto)
    {
        // ModelState.IsValid is always true now
        return Ok();
    }
}

public class MyDto
{
    [Required]
    public int Id { get; set; }
}

Remote Validations in Asp.Net MVC 4 with Example, , the values of above both the keys must be true. I would like to know how to display server-side validation to the user. For example, in the above code, it may be that the submitted bid amount is no longer sufficient. In that case, I would like to display a message to the user that they have been outbid etc.

In addition to @peco great answer this attribute will help if you will need to remove specific keys from ModelState:

public class ExceptPropertiesAttribute : ActionFilterAttribute
{
    private IEnumerable<string> _propertiesKeys;

    public ExceptPropertiesAttribute(string commaSeperatedPropertiesKeys)
    {
        if (!string.IsNullOrEmpty(commaSeperatedPropertiesKeys))
        {
            this._propertiesKeys = commaSeperatedPropertiesKeys.Split(',');
        }
    }

    public override void OnActionExecuting(ActionExecutingContext actionContext)
    {
        if (this._propertiesKeys != null)
        {
            foreach (var propertyKey in this._propertiesKeys)
            {
                if (actionContext.ModelState.ContainsKey(propertyKey))
                {
                    actionContext.ModelState.Remove(propertyKey);
                }
            }                
        }
    }
}

In addition, In .Net Core I can use ActionExecutingContext instead of HttpActionContext.

Usage:

[ExceptProperties("Id,Name")]
public ActionResult Post([FromBody] MyModelDTO itemDTO)
{
    //code
}

public ActionResult Put([FromBody] MyModelDTO itemDTO)
{
    //code
}

What is ModelState.IsValid valid for in ASP.NET MVC in NerdDinner , specific data without posting the entire form to the server. This article explains the basics of ASP.NET MVC server-side validation using the Data Annotation API. The ASP.NET MVC Framework validates any data passed to the controller action that is executing, It populates a ModelState object with any validation failures that it finds and passes that object to the controller. Then the controller actions can query the ModelState to discover whether the request is valid and react accordingly.

Model validation in ASP.NET Core MVC, Web API controllers don't have to check ModelState. To specify a custom error message for server-side validation of non-nullable types, MaxValidationDepth provides a way to stop validation early if the visitor recursion  Disable Model Validation in Asp.Net MVC. This is also nice because you can have client-side validation to prevent user errors and give you users a better

Model Validation in ASP.NET Web API, In ASP.NET Web API, you can use attributes from the System. In your controller action, you can check whether the model is valid: C# Copy. This article explains how to implement client-side validation in an ASP.NET MVC application. The validation implemented using jQuery and jQuery validation plug-in (jquery.validate.min.js and jquery.validate.unobtrusive.min.js). In the server-side validation, the page must be submitted via a postback to be validated on the server and if the

ASP.NET Enable Disable RequiredFieldValidator server side, In order to enable/disable server-side RequiredFieldValidator or validation of the The RequiredFieldValidator control belongs to System.Web.UI.WebControls. Server-side validation must be done whether we validate the received data on the client side. User could disable script in his browser or do something else to bypass client-side validation. In this case server-side validation must required to protect our data from dirty input. Server-side model validation technique. In MVC Razor, we can

Comments
  • Do you want to disable validation for a mvc controller or a web api controller?
  • we are using custom validation. but default validation execute? we want to disable default validation