The API Call
I am making a REST API call with the following message body:
{"Method":{"Token":"0","Value":"0"}}
400 Response
I am getting a 400 Bad Request response from the api with the following body:
{"Message":"The request is invalid.","ModelState":{"request.Method.Token":["Could not create an instance of type Namespace.ActionMethod. Type is an interface or abstract class and cannot be instantiated. Path 'ActionMethod.Token'."]}}
Code Information
The method which is receiving the api call looks like this:
public MethodResponse MakeMethodCall([Required] [FromBody] MethodRequest request)
MethodRequest
has a Method
property which is an abstract type.
public class MethodRequest
{
public ActionMethod Method { get; set; }
}
public abstract class ActionMethod
{
public string Token { get; set; }
}
public class FirstMethod : ActionMethod
{
public string Value { get; set; }
}
Question
How can I call the REST API and have it recognize that the type of Method
is FirstMethod
, instead of it trying to instantiate the abstract type ActionMethod
?
Note that I will need to have more implementations of ActionMethod
in the future (ie. SecondMethod
), so the solution will need to include an extensible ActionMethod
(interface would also be fine).
EDIT
It would also be reasonable to include an enum
to identify which implementation of ActionMethod
was being targeted by the API call.
I'm currently using a solution which has an ActionMethodType
enum and both FirstMethod
and SecondMethod
fields. I'm checking these fields based on the value of ActionMethodType
. This works, but I would like to have a single [Required]
field into which I could pass any implementation of ActionMethod
.