私が正しく理解すれば、あなたが実際に望むのはカスタムモデルバインダーだと思います。
public class NoBooleanModelBinder : IModelBinder
{
public bool BindModel(
HttpActionContext actionContext,
ModelBindingContext bindingContext)
{
if (bindingContext.ModelType != typeof(string))
{
return false; //we only want this to handle string models
}
//get the value that was parsed
string modelValue = bindingContext.ValueProvider
.GetValue(bindingContext.ModelName)
.AttemptedValue;
//check if that value was "true" or "false", ignoring case.
if (modelValue.Equals(bool.TrueString, StringComparison.OrdinalIgnoreCase) ||
modelValue.Equals(bool.FalseString, StringComparison.OrdinalIgnoreCase))
{
//add a model error.
bindingContext.ModelState.AddModelError(bindingContext.ModelName,
"Values true/false are not accepted");
//set the value that will be parsed to the controller to null
bindingContext.Model = null;
}
else
{
//else everything was okay so set the value.
bindingContext.Model = modelValue;
}
//returning true just means that our model binder did its job
//so no need to try another model binder.
return true;
}
}
次に、あなたがあなたのコントローラでこのような何かを行う可能性があります:
public object Post([ModelBinder(typeof(NoBooleanModelBinder))]string somevalue)
{
if(this.ModelState.IsValid) { ... }
}
が '' OnActionExecutingは(HttpActionContextするactionContext) 'WebAPIのフィルタであると' OnActionExecuting(ActionExecutingContextするactionContext)としてこのMVCやWebAPIのでしょうですMVCですか?間違ったタイプの 'ActionFilterAttribute'を持つ可能性があります。これがMVCかWebAPIかどうかを確認できれば、私は例を挙げることができます。 –
ウェブAPIです。私は、文字列を期待しているコントローラにブール値を送信すると、フレームワークは真のブール値を「真の」文字列に変換します。私はこれがフレームワークによってボックスから外されたと思いますが、私はこれを制御して、文字列の代わりにboolを送信すると、検証メッセージを表示したいと思います。 – user441365