asp.net mvc 4 - How to work with [Required] attribute & Model State Validation within Web Api Put -
currently facing, problem, when try call web api put method mvc api client, lets describe code structure bellow
test model (web api end)
public sealed class test { [required] public int id { get; set; } [required] public string name { get; set; } }
web api put method
public httpresponsemessage put(string token, ienumerable<test> data) { [...] return request.createresponse(httpstatuscode.ok); }
web api custom filter
public sealed class validatefilterattribute : actionfilterattribute { /// <summary> /// /// </summary> /// <param name="actioncontext"></param> public override void onactionexecuting(httpactioncontext actioncontext) { if (!actioncontext.modelstate.isvalid) { actioncontext.response = actioncontext.request.createerrorresponse( httpstatuscode.badrequest, actioncontext.modelstate); } } }
call web api client
async system.threading.tasks.task verifiedfaccount() { using (var client = gethttpclient()) { var url = string.concat("/api/verfication", "?token=", token); var data = new sampletest { id = 1, name = "xxx" }; var temp = new list<sampletest>(); temp.add(data); using (httpresponsemessage response = await client.putasjsonasync <ienumerable<sampletest>>(url, temp).configureawait(false)) { if (response.issuccessstatuscode) { } } }
client code unable execute api call (even placed debug point within web api put method, unable hit debug point) & got bellow error response
{statuscode: 500, reasonphrase: 'internal server error', version: 1.1, content: system.net.http.streamcontent, headers: { pragma: no-cache x-sourcefiles: =?utf-8?b?stpcrgv2qxjlyuxvy2fsxenptvbbtlkglsbqu1agufjpskvdvfncrs1bdxrob3jpdhkglsbbdxn0cmvsaxlhxfnvdxjjzunvbnryb2xcvhj1bmtcmdygrgvjidiwmtncrs1bdxrob3jpdhkuqxbpic0gmjaxm1xfyxv0ag9yaxr5lldlyi5bcgkuuhjlc2vudgf0aw9utgf5zxjcyxbpxfntc2zby2nvdw50vmvyzmljyxrpb24=?= cache-control: no-cache date: mon, 14 apr 2014 11:23:27 gmt server: microsoft-iis/8.0 content-length: 2179 content-type: application/json; charset=utf-8 expires: -1 }}
but when remove [required]
test model (web api end). above described client code execute successfully.
please tell me reason of kind of confusing behavior ?
the issue facing might because of behaviour of default configuration when comes data validation. have required
attributed on non-nullable type , since int
can't null, have value (the default of 0
) if incoming request not provide value.
in these cases, model validator throw exception because doesn't make sense have required
attribute on property can't null.
the straightforward way change setting on mvc application:
dataannotationsmodelvalidatorprovider .addimplicitrequiredattributeforvaluetypes = false;
this rid of error thrown framework. introduce problem value of 0
when request not include property. makes more sense have integer of nullable<int>
. required
attribute able handle null
value , know whether or not incoming request included property
public sealed class test { [required] public int? id { get; set; } [required] public string name { get; set; } }
Comments
Post a Comment