When transmitting data to a REST API, you need to keep in mind that client and server are basically unrelated in terms of technology and therefore use a common ground that a lot of programming languages support. In your case, there might be a .NET client and a .NET server, but it could well be that a Javascript client calls your API that does not know of the Type class. Even if you have .NET client and a .NET server, the client could send a type that the server is unaware of.
So you need to find a common ground that is supported by various programming environments and is flexible enough to also cope with the situation that the client sends data that the server cannot map to a type.
One way would be to not transmit a Type directly, but transmit the type name as a string. The server could then try to load the type directly. If the type can be found, it could work with the type, if not, it would return a BadRequestResult to notify the client of the invalid data.
[HttpPost]
[Route("SendModelPayload")]
public async Task<IActionResult> SendModelPayload([FromBody] string typeName)
{
// Try to load type
var type = Type.GetType(typeName);
if (type == null)
return BadRequest();
// Do some stuff
return Ok("test");
}
The client would send a type name instead of a real type e.g.
var res = httpClient.PostAsJsonAsync(@"/api/ModelPayload/SendModelPayload",
typeof(Student).AssemblyQualifiedName).Result;
By transmitting the AssemblyQualifiedName, the type name is very specific, so the server needs to be able to load the exact same type, but fail if it cannot laod the type.