madaxe2020
Well-known member
- Joined
- Sep 7, 2020
- Messages
- 50
- Programming Experience
- 5-10
Were building a bunch of REST API End Points, we decided that we wanted a generic transportation object to be returned from all Rest calls that contains the data and other attributes.
The problem is that all of the models implement a common interface "IModlesBase" to support this. Now I don't know how to Deserialize since the child of the generic return object is an interface.
I know I have to tell the NewtonSoft what the concrete type is but I don't know how.
can anybody please show me how to get around this?
thanks
madaxe
The problem is that all of the models implement a common interface "IModlesBase" to support this. Now I don't know how to Deserialize since the child of the generic return object is an interface.
I know I have to tell the NewtonSoft what the concrete type is but I don't know how.
can anybody please show me how to get around this?
thanks
madaxe
C#:
GenericReturnModel claimTerms = await Environments.Get<GenericReturnModel>("/Url/v1")
Rest Call:
public static async Task<T> Get<T>(string url)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(apiBasicUri);
var result = await client.GetAsync(url);
result.EnsureSuccessStatusCode();
string resultContentString = await result.Content.ReadAsStringAsync();
T resultContent = JsonConvert.DeserializeObject<T>(resultContentString);
return resultContent;
}
}
GenericReturnModel:
public class GenericReturnModel
{
public IModelsBase? ReturnModel { get; set; } = null;
}
IModelsBase:
public interface IModelsBase
{
}
public class ModelsBase : IModelsBase
{
}
C#:
public class EnvironmentModel : IModelsBase{}
public class EnvironmentVariableModel : IModelBase{}