c# - oData getter for Dynamicproerties -


i have class dynamicproperties (open type) exposing via odata. per understanding, if user queries 1 of dynamicproperties request goes getdynamicproperty method (or there other better way ?).

  1. how property user trying access ?. @ present parsing uri using odatauriparser. right approach ?. there other better way it?

  2. when return not able return value of property stored type object in dictionary. @ present returning string. want return actual type or other means preserve actual type, how do ?

    public class bookscontroller : odatacontroller {               public ihttpactionresult getdynamicproperty([fromodatauri]string key)      {       try      {         odatauriparser uriparser = new odatauriparser(webapiconfig.getedmmodel(), new uri(request.requesturi.pathandquery, urikind.relative));         openpropertysegment propertysegment = uriparser.parsepath().lastsegment openpropertysegment;         if(propertysegment == null || string.isnullorempty(propertysegment.propertyname))         {             return notfound();         }         string property = propertysegment.propertyname;         book book = getbook(key);         return ok(book.dynamicproperties[property].tostring());      }      catch(exception)      {         return notfound();      }    } }  public class book {    public string isbn { get; set; }    public string title { get; set; }    public press press { get; set; }    public dictionary<string, object> dynamicproperties { get; set; } } 

for #1, think shouldn't that. there 2 ways can use query dynamic property since web api odata v5.6

  1. convention routing

it should work define method named "getdynamicproperty" in bookscontroller. example:

public ihttpactionresult getdynamicproperty([fromodatauri] string key, [fromodatauri] string dynamicproperty) {  ... } 

where, parameter name should named dynamicproperty.

  1. attribute routing

it should work define random method in controller odataroute attribute on it. example:

[httpget] [odataroute("books({key})/press/{pname:dynamicproperty}")] public ihttpactionresult xxxxdynamicpropertyxxxx([fromodatauri] string key, [fromodatauri] string pname) { ... } 

the template dynamic property should "{anyname:dynamicproperty}"

for #2, please add following method in controller,

private ihttpactionresult ok(object content, type type) {     var resulttype = typeof(oknegotiatedcontentresult<>).makegenerictype(type);     return activator.createinstance(resulttype, content, this) ihttpactionresult; } 

and call in http response method. example:

[httpget] [odataroute("books({key})/press/{pname:dynamicproperty}")] public ihttpactionresult xxxxdynamicpropertyxxxx([fromodatauri] string key, [fromodatauri] string pname) {     book book = _books.firstordefault(e => e.isbn == key);     if (book == null)     {         return notfound();     }      object value;     if (!book.press.dynamicproperties.trygetvalue(pname, out value))     {        return notfound();     }       return ok(value, value.gettype()); } 

test:

if use sample "books" in http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/use-open-types-in-odata-v4

i query

http://localhost/odata/books('978-0-7356-7942-9')/authors    

i can result as:

{   "@odata.context":"http://localhost/odata/$metadata#collection(edm.string)","va lue":[     "leonard g. lobel","eric d. boyd"   ] } 

while, query:

http://localhost/odata/books('978-0-7356-7942-9')/press/address 

i can result as:

{   "@odata.context":"http://localhost/odata/$metadata#books('978-0-7356-7942-9')/ press/address","city":"redmond","street":"one microsoft way" } 

hope can you. thanks.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -