2

3 天以来,我试图将我的对象发送到我的 webapi。我只是不能从 c# 中做到这一点:/

我正在尝试 PostAsync 方法,我正在尝试发送 json(但后来我无法反序列化),我正在尝试使用 JObject 但无法对其执行任何操作。我只是不知道该怎么做。

我得到了非常简单的方法

    public string PostMethod(Location param)
    {
        var test = param;

        return test.name;
    }

如何从 c# 发送 Location 对象,这样我就不会在 webapi 中获得 null ?我唯一能做的就是发送一个简单的类型,比如'string'。但是我的方法都不适用于更复杂的对象。

位置对象下方

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; }
}
4

1 回答 1

4

您是否从 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;
于 2012-10-03T08:30:28.737 回答