c# - ASP.net WebApi Parameter Binding to complex type from URI -


i want create rest webservice using asp.net webapi on .net 4.5

urls should of format this:

/values?age=55&height=176&race=1&weight=80&gender=male&id=800001 

the associated controller looks this:

[httpget] public string get(int id, [fromuri]demographics demographics) { // etc.} 

where demographics custom object dto properties. however, contains 1 property has type of custom enum:

enum gender {  female = 0,  male } 

the default mapping works fine if url of above format. however, of course need check whether parameters provided url correct.

asp.net webapi (afaik) per default tries map each parameter based on assumed type (or if can converted type). if can't find matching value in uri appears assume 0.

now takes me unfortunate situation 0 definition still valid value gender demographics.gender (0 ~ gender.female).

the simplest solution change enum 0 "indeterminate" state check for. however, can not change enum. create own overload demographics object rather not this, because think there must nicer way.

can explicitely bind gender parameter parameter in uri , throw exception if not submitted?

i read type converters have work on uri string , think have implement lot functionality webapi apparently has.

please keep in mind have work on uri , can not use request body.

how making enum nullable this?

public class demographics {     public gender? gender { get; set; } } 

inside action method, can check if demographics.gender null or not.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -