asp.net web api - Inject Default value for Web-Api Method parameter when its null -


i have web api method like;

[httpget, route("users")] public httpresponsemessage getusers([fromuri] usersearchdto searchparams) {} 

the searchparams optional parameter when pass no search values , use http://api-uri/users becomes null , have add check in body of method avoid null reference exception.

is there way using actionfilters or else inject default value parameter of web api method avoid

if (searchparams == null){   searchparams = new usersearchdto () } 

not know of. honestly, you're doing pretty it. if want make bit neater, yo can condense null check 1 line so:

searchparams = searchparams ?? new userseachdto(); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -