Asp.net web api 以ModelClass对象作为参数发布到WebAPI

Asp.net web api 以ModelClass对象作为参数发布到WebAPI,asp.net-web-api,xamarin,xamarin.forms,Asp.net Web Api,Xamarin,Xamarin.forms,我们的web api如下所示: [HttpPost] public CustomAuthenticateModel AuthenticateByUsername(LoginModel model) { return employeeService.AuthenticateByUsername(model.Username, model.AdDomain, model.IsAdAuthentication); } 在我的PCL项目中,我试图通过以下方式访问: try { Http

我们的web api如下所示:

[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都使用模型类对象作为参数。正确的方法是什么?我正在尝试为此项目使用MVVM帮助程序库。

在发出请求之前序列化您的对象。Javascript序列化程序应与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());
 }
var jsonRequest=Newtonsoft.Json.JsonConvert.SerializeObject(参数);
var content=newstringcontent(jsonRequest,Encoding.UTF8,“text/json”);
//url是api,l是您要传递的对象
var response=wait client.PostAsync(url,l);
if(响应。IsSuccessStatusCode)
{
//R是对象类型,在本例中为LoginModel
result=Newtonsoft.Json.JsonConvert.DeserializeObject(wait response.Content.ReadAsStringAsync());
}

您的apiClient是什么?您是否为此使用了库?我也遇到了同样的问题。您找到解决方案了吗?