query string - Web API: QueryString model binding rules with DataAnnotations -


i'm becoming quite confused how model binding, , model validation occurs in web api. majority of actions receive objects through post requests, , models have variety of validationattributes applied them, such requiredattribute, minlengthattribute, stringlengthattribute etc.

one of actions, however, request, , has signature similar this:

public searchresultcollection search([fromuri]searchrequest request) 

originally, looked below, discovered can create single class encapsulates arguments come in on query string:

public searchresultcollection search(string searchpath, string searchvalue, int limit, string sortby) 

using latter, unable form of validation working on arguments. example, applying requiredattribute searchpath parameter seemingly did nothing. prompted change action signature, , creation of searchrequest class, looks this:

public class searchrequest {     public searchrequest()     {         sortby = searchsortingkey.id;     }      [required(errormessage="a search path must specified.")]     public string searchpath{ get; set; }      [required(errormessage="a search value must specified.")]     public string searchvalue{ get; set; }      [required(errormessage="a limit specified.")]     public int limit { get; set; }      public searchsortingkey sortby { get; set; } } 

using method, seems though requiredattributes recognised (they cause model validation fail), error message returned whilst performing model validation isn't 1 specified in requiredattributes above.

i don't have problem when doing model validation post requests, , don't understand why behaves differently when model comes via query string.

can shed light on this? i'd understand how validate arguments passed in on querystring - assumed there other way besides performing validation in action body.

i read article here, doesn't explain how or why model validation differs when model comes in on query string.

the issue describe (was) bug in asp.net webapi resolved. can find details @ http://aspnetwebstack.codeplex.com/workitem/1471.

for string properties should able use [required] add parameter allowemptystrings=false. required work-around (as described bug reporting developer) non-string properties.

and may want make int property nullable (int?) if you're using json deserialization; without making int? newtonsoft.json converts , empty string property 0.

/searchrequest?searchpath=[some string]&searchvalue=[some string]&sortby=[some string]&limit=


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -