I have two simple console c# application. First realize web api servece (only one web api controller with one get method).Second application is client for first application. At client application I want to get some list from first application. This list is the list of derive classes. When i activate get method from Internet Browser, - actual xml data with derived classes is appears. But when i read list form c# client, i get the list of base classes (i use response.Content.ReadAsAsync)
// Data classes
public class TestBase
{
public string P1 {get;set;}
public bool P2 { get;set;}
}
public class Test1 : TestBase
{
public bool P3 { get; set; }
}
public class Test2 : TestBase
{
public bool P4 { get; set; }
}
// Model
[KnownType(typeof(Test1))]
[KnownType(typeof(Test2))]
public class ModelData
{
public List<TestBase> Testlist { get; set; }
}
// Web Api Controller
public class TagsController : ApiController
{
public ModelData GetModel()
{
var model = new ModelData();
model.Testlist = new List<TestBase>(){new Test2(),new Test1()};
return model;
}
}
// this is selfhosting settings
//...
_selfHostConfiguraiton = new HttpSelfHostConfiguration(_url);
_selfHostConfiguraiton.Routes.MapHttpRoute( name: "DefaultApiRoute",
routeTemplate: "api/{controller}/{action}",
defaults: null);
_server = new HttpSelfHostServer(_selfHostConfiguraiton);
_task = _server.OpenAsync();
//...
// Client code on application №2
//...
HttpResponseMessage response = Client.GetAsync("api/Tags/GetModel").Result;
if (response.IsSuccessStatusCode)
{
var modelData = response.Content.ReadAsAsync<ModelData>().Result;
// So modelData.Testlist is list of two base classes {TestBase, TestBase}.
// is any possibility for deserialize modelData.Testlist to {Test2, Test1}???
}
//...
Aucun commentaire:
Enregistrer un commentaire