【发布时间】:2016-07-26 01:36:18
【问题描述】:
客户端向 web api 方法发出获取请求并获取一个对象作为响应,问题是我无法反序列化这个对象..
客户端方法,向web api发出get请求
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:57752");
HttpResponseMessage response = client.GetAsync("api/Auth/Login/" + user.Username + "/" + user.Password).Result;
JsonResult result = null;
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsAsync<JsonResult>().Result;
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
User validUser = json_serializer.Deserialize<User>(result.Data.ToString());//Throws Exp.
}
我想简单的把这个从api返回的对象实例放到validUser..
错误信息:
无法将“System.String”类型的对象转换为类型 'MongoDB.Bson.ObjectId'
这里是模型:
public abstract class EntityBase
{
[BsonId]
public ObjectId Id { get; set; }
}
public class User : EntityBase
{
//public string _id { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
[DataType(DataType.Text)]
[Display(Name = "Username")]
public string Username { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
public void EncryptPassword()
{
Password = Encrypter.Encode(this.Password);
}
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
【问题讨论】:
标签: asp.net-mvc asp.net-web-api