I have a simple client that sends data to the web api server through a generic type
The following structure exists on both server and client
public interface IRequest<T>
{
string ApiKey { get; set; }
T RequestObject { get; set; }
}
public class UserRequest : IRequest<UserDetail>
{
public string ApiKey { get; set; }
public UserDetail RequestObject { get; set; }
}
I send a request to the server using the following syntax
var client = new RestClient(WebConfigurationManager.AppSettings["apiUri"]);
var profileRequest = new UserRequest
{
ApiKey = "xxxxx",
RequestObject = new UserDetail {Password = txtPassword.Text, UserName = txtUserName.Text}
};
var request = new RestRequest("UserRequest/PostUserDetail", Method.POST);
request.AddObject(profileRequest);
Now the interesting thing is that on the Server side, the UserDetail object does not get de-serialised properly, it just contains NULL, the ApiKey property is set correctly though
public UserDetail PostUserDetail(UserRequest userRequest)
{
return new UserDetail { Password = userRequest.RequestObject.Password, UserName = userRequest.RequestObject.UserName };
}
from the above, userRequest.ApiKey is set, however, userRequest.RequestObject is always NULL on the server API
Are generic types not supported? if not, does anyone have any pointers as to how I could modify the Deserializer on the server side? I have checked to see if the object is constructed properly on the client side and it is.
Many Thanks
This Web Api scenario is supported. I have tried repro’ing your WebAPI by creating a UserRequest ApiController with PostUserDetail, and I was able to get the expected ‘Password’ and ‘UserName’ in the response:
On the client side, I would recommend you to use HttpClient instead. Here is my client repro: