特定のModelErrorを返すValidationAttributeを特定しようとしているところに問題があります。私のようなモデルを取る私のWeb APIのエンドポイントがあります。ModelErrorを返すValidationAttributeを特定する方法
public class MyClass
{
[Required]
[Range(0, 3)]
public int? Number { get; set; }
[Required]
[Range(0, 3)]
public int? NumberTwo { get; set; }
}
と、ModelStateが有効であることを確認するフィルタ。
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
IEnumerable<ModelError> errors = actionContext.ModelState.Values.SelectMany(s => s.Errors);
// ...
}
}
}
私は、ModelErrorには2つのプロパティがあります。 ErrorMessageは文字列型で、ExceptionはException型です。 ValidationAttribute [必須]または[Range(0、3)]が文字列操作を行わずにエラー応答を返したかどうかを判断するための強力な型付きの方法が欲しいです。私がよく知らないこれらの属性を使用してカスタムプロパティを返す方法はありますか?
クライアントが最終的な目標は、次のようなAPIからの応答を生成することであろう、このような
{
"NumberTwo":10
}
としてモデルを投稿した場合。
{
"supportCode" : "1234567890",
"errors" : [{
"code" : "Missing",
"message" : "The Number field is required."
}, {
"code" : "Invalid",
"message" : "The field NumberTwo must be between 0 and 3."
}]
}