【发布时间】:2021-08-28 22:25:23
【问题描述】:
我正在将multipart/form-data 形式的数组字段发送到服务器,但是当我调用该数组中的变量时,它给了我一个错误。
代码
MultipartFormDataContent multiContent = new MultipartFormDataContent();
multiContent.Headers.ContentType.MediaType = "multipart/form-data";
string email = "mail@example.com";
string firstName = "Andrie";
string lastName = "Tarkovsky";
var fields = "[{\"propertyName\": \"email\", \"propertyValue\": " + email + ", \"propertyLabel\": \"Email\"}, {\"propertyName\": \"firstName\", \"propertyValue\": " + firstName + ", \"propertyLabel\": \"First Name\"}, {\"propertyName\": \"lastName\", \"propertyValue\": " + lastName + ", \"propertyLabel\": \"Last Name\"}]";
multiContent.Add(new StringContent(fields), "properties");
错误
"\"properties must be a `array` type, but the final value was: `null` (cast from the value `\\\"[{\\\"propertyName\\\": \\\"email\\\", \\\"propertyValue\\\": mail@example.com, \\\"propertyLabel\\\": \\\"Email\\\"}, {\\\"propertyName\\\": \\\"firstName\\\", \\\"propertyValue\\\": Andrie, \\\"propertyLabel\\\": \\\"First Name\\\"}, {\\\"propertyName\\\": \\\"lastName\\\", \\\"propertyValue\\\": Tarkovsky, \\\"propertyLabel\\\": \\\"Last Name\\\"}]\\\"`).\\n If \\\"null\\\" is intended as an empty value be sure to mark the schema as `.nullable()`\""
更新
public class Item
{
public string name { get; set; }
public string value { get; set; }
}
【问题讨论】:
-
请分享“multiContent”的类型
-
您需要添加更多代码,例如
multiContent的类型。field属性在我看来更像 JSON? -
添加在上面,请看一下..谢谢
-
“它给了我一个错误”——“它”是谁?是编译器错误吗?例外?服务器响应错误?
-
您不应该尝试手动创建 JSON。
标签: c# .net xamarin xamarin.forms