我尝试从 Azure 创建一个新的移动服务,并且数据已被 Json 正确公开。
https://lifehope.azure-mobile.net/tables/USERPF
USERPF 是一个示例表。
为了简化问题,我只是将权限修改为“所有人”。
问题是下面列出的代码不起作用。错误消息是:远程服务器返回错误: 当我点击插入按钮在 USERPF 中插入新记录时未找到...
private void butInsert_Click(object sender, RoutedEventArgs e)
{
USERPF item = new USERPF();
item.Column1 = 789;
item.Column2 = 789;
WebClient wc = new WebClient();
wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
//wc.Headers["X-ZUMO-APPLICATION"] = "";
wc.UploadStringCompleted += (ss, arg) =>
{
if (arg.Error == null)
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show(arg.Error.Message);
}
};
wc.UploadStringAsync(
new Uri("https://lifehope.azure-mobile.net/tables/USERPF/"),
"POST", JsonHelper.ObjectToJson(item, typeof(USERPF)));
}
//USERPF.cs
public class USERPF
{
[System.Runtime.Serialization.IgnoreDataMember()]
public int id { get; set; }
[System.Runtime.Serialization.DataMember()]
public int Column1 { get; set; }
[System.Runtime.Serialization.DataMember()]
public int Column2 { get; set; }
}
//JsonHelper.cs
public static string ObjectToJson(object obj, Type type)
{
try
{
//Create a stream to serialize the object to.
MemoryStream ms = new MemoryStream();
// Serializer the User object to the stream.
DataContractJsonSerializer ser = new DataContractJsonSerializer(type);
ser.WriteObject(ms, obj);
byte[] json = ms.ToArray();
ms.Close();
return Encoding.UTF8.GetString(json, 0, json.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return string.Empty;
}
}