连续 3 天我试图将我的对象发送到我的 webapi。我只是无法从 C# 做到这一点:/
我正在尝试 PostAsync 方法,我正在尝试发送 json (但后来我无法反序列化),我正在尝试使用 JObject 但无法对其执行任何操作。我只是不知道该怎么做。
我有一个简单的方法
public string PostMethod(Location param)
{
var test = param;
return test.name;
}
如何从 C# 发送 Location 对象,这样我就不会在 webapi 中得到 null ?我唯一实现的就是发送一个简单的类型,如“字符串”。但我的方法都不适用于更复杂的对象。
位于位置对象下方
public partial class Location
{
public Location()
{
this.Category = new HashSet<Category>();
}
public int id { get; set; }
public Nullable<System.DateTime> create_date { get; set; }
public Nullable<System.DateTime> modify_date { get; set; }
public Nullable<int> create_user_id { get; set; }
public Nullable<int> modify_user_id { get; set; }
public string name { get; set; }
public string description { get; set; }
public Nullable<int> photo_id { get; set; }
public string www { get; set; }
public string count_data { get; set; }
public string status { get; set; }
public Nullable<double> latitude { get; set; }
public Nullable<double> longitude { get; set; }
public string city { get; set; }
public string country { get; set; }
public string zip { get; set; }
public string telephone { get; set; }
public string street { get; set; }
public virtual AzureMedia AzureMedia { get; set; }
public virtual Users Users { get; set; }
public virtual Users Users1 { get; set; }
public virtual ICollection<Category> Category { get; set; }
}
最佳答案
您是否安装了 NuGet 的最新 Web API?为此,您还需要 Json.NET。
这应该可以解决问题:
Location loc = new Location( ..... );
var jsonFormatter = new JsonMediaTypeFormatter();
HttpContent content = new ObjectContent<Location>(loc , jsonFormatter);
HttpResponseMessage responseMessage = client.PostAsync(uri, content).Result;
关于c# - 如何将对象从 C# webapp 发送到 asp.net webapi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12704571/