I have a method at a controller which takes an object, And i need to deserialize differently depending on how i create the service at DI.
The method is bool SetSnmpRequest(Object obj);
Then in the controller
public IActionResult Set([FromBody]object details)
{
bool setSuccesses = _devicesManager.SetSnmpRequest(details);
if (setSuccesses)
return Ok();
return BadRequest();
}
This is the JSON passed from the client {"Name":"Power" ,"Value":"MED","ChannelIndex":"1"}
And the object i`m trying to deserialize to
public class SetRequestModel
{
public string Name { get; set; }
public string Value { get; set; }
public int ChannelIndex { get; set; } = 0;
}
The desirialization SetRequestModel details = JsonConvert.DeserializeObject<SetRequestModel>((string)obj);
The problem at all that is that JsonConvert.DeserializeObject takes a string and i get an exception when casting the object to a string
System.InvalidCastException: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.String'.