ASP Net不能接受带方括号[]的json

时间:2018-12-12 21:31:08

标签: c# json

这是我如何在ASP网络中创建寄存器的方法

vagrant up

我收到错误400无效请求无法反序列化当前json数组,例如1,2,3 ...如果我将从string中删除第一个和最后一个括号,则它可以工作。但这不是标准的json格式。如何解决这个问题?

这是我的字符串的样子:

public class RegisterBindingModel { public string Email { get; set; } public string Password { get; set; } public string ConfirmPassword { get; set; } } async void Register(string email,string password, string confirmPassword) { List<RegisterBindingModel> mItems = new List<RegisterBindingModel>(); mItems.Add(new RegisterBindingModel { Email = email, Password = password, ConfirmPassword = confirmPassword }); var json = JsonConvert.SerializeObject(mItems); using (var client = new HttpClient()) { client.Timeout = TimeSpan.FromMilliseconds(20000); var response = await client.PostAsync("http://localhost:49826/api/Account/Register", new StringContent(json, Encoding.UTF8, "application/json")); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); Console.Write((int)response.StatusCode); } else { string content = await response.Content.ReadAsStringAsync(); Console.Write((int)response.StatusCode); } } } private void button1_Click(object sender, EventArgs e) { Register("dimitris2@in.gr", "Password1?", "Password1?"); }

1 个答案:

答案 0 :(得分:4)

看起来您正在将单元素数组传递给服务器,而服务器希望单个元素本身出现。这可以通过序列化对象而不将其添加到列表中来解决:

var json = JsonConvert.SerializeObject(new RegisterBindingModel {
    Email = email,
    Password = password,
    ConfirmPassword = confirmPassword
});
相关问题