2012-10-03 39 views
2

3天我想把我的對象發送到我的webapi。我只是不能做到這一點從C#:/我怎麼能從c#webapp發送對象到asp.net webapi

我正在嘗試PostAsync方法,我試圖發送JSON(但後來我不能反序列化),我試圖使用JObject,但不能無能爲力它。我只是不知道該怎麼做。

我RLY簡單的方法

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

     return test.name; 
    } 

我怎麼能發送從C#Location對象,所以我不會在得到的WebAPI空?只有我已經完成的事情是發送一個簡單的類型,如「字符串」。但是我的方法都不適用於更復雜的對象。

以下Location對象

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

是否安裝從的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; 
+0

是什麼在此代碼PARAM? – Fixus

+0

@Fixus:你的代碼中的'param' –

+0

@Remy但param在我的webapi中,我需要從客戶端應用程序發送它。這就是爲什麼我問我的客戶端應用程序中的「參數」是什麼 – Fixus

相關問題