c# - To Create Action That Accepts a Parameter Of Any Type Derived From The Parameter Type (Polymorphic Parameter) -


i want build dynamic structure client ask server in web api. have tried use following code deal question, however, isn't working.

  1. how can send generic type <travel> service
  2. how can change server code (or need change client/server)?

ps:thank patience if have read question end.

client code

var serializer = new javascriptserializer();     var product = new travel() { travel_desc = "select * travel" };     var jsontext = serializer.serialize(product);     var client = new httpclient();     client.baseaddress = new uri("http://localhost:65370/");      client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json"));     stringcontent content = new stringcontent(jsontext, encoding.utf8, "application/json");       var z = client.postasync<travel>("api/bb", product, new jsonmediatypeformatter()).result; 

server code, not working

public ihttpactionresult post< t > (object x) t : new()     {                     ........................     } 

by way okay don't know how send < t > server

public ihttpactionresult post(object x)     {                     ........................     }      

error message
client call server, server getting error message " statuscode: 404, reasonphrase: 'not found' "

 var z = client.postasync < travel > ("api/dd", product, new jsonmediatypeformatter()).result; <--client      public class ddcontroller< t > : apicontroller {public virtual void post() {   ... }  } <---server            // sorry , english isn't , try use code tell how want  // in format situations,i create 2 controller when have 2 models(ex: users/product) , following (client)                 var = client.postasync("api/users", users, new jsonmediatypeformatter()).result;                 var b = client.postasync("api/product", product, new jsonmediatypeformatter()).result;  //and when users , product controllers created post code should following (server)                 public ihttpactionresult postusers(users travel) {}                 public ihttpactionresult postproduct(product travel) {}     //now want create 1 controller above  follwing                   var b = client.postasync<users/product>("api/all", product, new jsonmediatypeformatter()).result;(client)                   public ihttpactionresult post<t>(object forall) t : new() {} (server) 

json.net, web api json serializer, able send type information when serializing object, , use same information deserialize it.

the trick uses including $type property first property of json object.

if want use technique, need have base class or interface, example itravel, inherit possible classes it, , use base class or interface parameter type, so:

public interface itravel {   public int travelid { get; set; } }  public class traveltypea : itravel {   public int travelid { get; set; }   public string destination { get; set; } }  public class traveltypeb : itravel {   ... }  [httppost] public object postmeatravel(itravel travel) {     // check type travel "is" or ".gettype()" } 

you need instruct json include type information when (de)serializing itravel objects. (json typename handling):

jsonserializersettings serializersettings    = globalconfiguration.configuration.formatters     .jsonformatter.serializersettings;  serializersettings.typenamehandling = typenamehandling.auto; 

and have post json typeinformation, this:

{   $type: 'sampleapp.traveltypea, sampleapp',   travelid: 22,   destination: 'la almunia de doña godina' } 

when so, json.net use type information create traveltypea object, , pass parameter action, expects itravel. inside action can check type of received parameter if need so, this: if (travel.gettype().name == "traveltypea") { ... }

look @ q&a more information on how that, how works, , advantages , drawbacks of method , alternative way of doing it: deserialising json derived types in asp.net web api

note: can use excellent postman complement chrome test web api methods


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -