使用JObject将数据传递给webapi,如何对从JObject返回的对象进行模型验证?我正在使用angurlarjs进行绑定,并使用DTO作为我的模型。
Dim db As DAO.Database
Set db = DBEngine.Workspaces(0).OpenDatabase("FullPathAndFileNameOfDb")
db.Execute "qry325_NMC_Final", dbFailOnError
Set db = Nothing
答案 0 :(得分:0)
如果要验证输入数据,请考虑使用自定义ActionFilterAttributes进行装饰。
[RequiresJsonBody("SchoolBranch","SchoolPolicy")]
public HttpResponseMessage InsertSchoolBranch(JObject jsonData)
{
// Stuff...
}
或者可能使用一些元组作为模型
public class RequiresJsonBody : ActionFilterAttribute
{
private string paramName;
public RequiresJsonBody (string paramName)
{
this.paramName = paramName;
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
IDictionary<string, string> errors = new Dictionary<string, string>();
// Validate incoming. Add key / error messages to dictionary...
foreach (var err in errors)
{
actionContext.ModelState.AddModelError(err.Key, err.Value);
}
if (!actionContext.ModelState.IsValid
&& errors.Keys.Count > 0)
{
actionContext.Response
= actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest,
String.Join(" ", errors.Values.ToArray()));
}
}
}