I'm experimenting with the MVC4 WebApi for building rest/json services.
In my controller I have the method:
public HttpResponseMessage Post(dynamic message)
This contains a single object which always has two fields, type and action.
Action is either Create or Cancel.
I wrote three classes, Message, CreateMessage and CancelMessage, Message is the base class the other two inherit.
After reading the blurb on dynamic
I thought I'd be able to do this:
public void ProcessMessage(dynamic message)
{
switch ((string)message.action)
{
case "CREATE":
ProcessCreateMessage(message);
break;
case "CANCEL":
ProcessCancelMessage(message);
break;
}
}
private void ProcessCancelMessage(CancelMessage message)
{
//Cancell
}
private void ProcessCreateMessage(CreateMessage message)
{
//Create
}
But I just get either a message about there not being an overload (implicit cast) or "Cannot convert type 'Newtonsoft.Json.Linq.JObject' to 'CancelMessage'"
The classes
public class Message
{
public string type { get; set; }
public string action { get; set; }
}
public class CancelMessage : Message
{
public string ref { get; set; }
public string message { get; set; }
}
The Json:
{
"type" : "type",
"action" : "cancel",
"ref" : "RefNo",
"message" : "a message"
}
What am I not getting here?