Model view controller 如何禁用服务器端验证mvc web api控制器
如何禁用服务器端验证mvc web api控制器。请告诉我使用自定义验证的简单方法。尝试使用Model view controller 如何禁用服务器端验证mvc web api控制器,model-view-controller,asp.net-web-api,Model View Controller,Asp.net Web Api,如何禁用服务器端验证mvc web api控制器。请告诉我使用自定义验证的简单方法。尝试使用 [ValidateInput(false)] 使用操作方法时,web api没有将禁用验证的ValidateInput属性,但您可以轻松定义一个属性来重置ModelState: public class ValidateInput : ActionFilterAttribute { private readonly bool _enableValidation; public Val
[ValidateInput(false)]
使用操作方法时,web api没有将禁用验证的
ValidateInput
属性,但您可以轻松定义一个属性来重置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();
}
}
}
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);
}
}
}
}
}
然后在控制器中使用:
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; }
}
[DisableModelValidator]
public class SomeController : ApiController
{
public IHttpActionResult Post(MyDto dto)
{
// ModelState.IsValid is always true now
return Ok();
}
}
来自
@peco
的答案仅清除验证,但验证仍会运行
要禁用控制器的验证,可以使用自定义IControllerConfiguration
属性清除特定控制器的ModelValidatorProvider
public class DisableModelValidatorAttribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings settings,
HttpControllerDescriptor descriptor)
{
settings.Services.Clear(typeof(ModelValidatorProvider));
}
}
只需将该属性应用于控制器:
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; }
}
[DisableModelValidator]
public class SomeController : ApiController
{
public IHttpActionResult Post(MyDto dto)
{
// ModelState.IsValid is always true now
return Ok();
}
}
另见:
和除了@peco great answer之外,如果您需要从
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();
}
}
}
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);
}
}
}
}
}
我们正在使用自定义验证。但是否执行默认验证?我们要禁用默认验证是否要禁用mvc控制器或web api控制器的验证?