2

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

1
  • Do you want to disable validation for a mvc controller or a web api controller? Commented Jun 9, 2016 at 6:44

4 Answers 4

1

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://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/configuring-aspnet-web-api and disable default validation in Asp.Net WebAPI

Sign up to request clarification or add additional context in comments.

Comments

1

try to use

[ValidateInput(false)]

with action method

1 Comment

we are using custom validation. but default validation execute? we want to disable default validation
0

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; }
}

Comments

0

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
}

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.