我们有以下网址:
[HttpPost]
public CustomAuthenticateModel AuthenticateByUsername(LoginModel model)
{
return employeeService.AuthenticateByUsername(model.Username, model.AdDomain, model.IsAdAuthentication);
}
在我的PCL项目中,我试图通过以下方式访问:
try
{
HttpResponseMessage response = null;
LoginModel l = new LoginModel();
l.Username = model.Email;
response = await apiClient.PostAsJsonAsync(uri, l); // Exception is fired at this line
}
catch(exception etc){}
每次我都得到例外:
ex = {System.TypeInitializationException:'System.Net.Http.FormattingUtilities'的类型初始值设定项引发了异常。 ---> System.NotImplementedException:未实现方法或操作。 在System.Runtime.Serialization.XsdDataContractExporte ...
这是一个现有项目,所有API都使用Model Class对象作为参数。这样做的正确方法是什么?我正在尝试为这个项目使用MVVM帮助程序库。
答案 0 :(得分:0)
在发出请求之前序列化您的对象。 Javascript Serializer应该和Newtonsoft序列化程序一样好用
var jsonRequest = Newtonsoft.Json.JsonConvert.SerializeObject(argument);
var content = new StringContent(jsonRequest, Encoding.UTF8, "text/json");
//url is the api, l is your object that you are passing
var response = await client.PostAsync(url, l);
if (response.IsSuccessStatusCode)
{
//R is your object type, in this case LoginModel
result = Newtonsoft.Json.JsonConvert.DeserializeObject<R>(await response.Content.ReadAsStringAsync());
}