2016-09-28 80 views
0

我是web api的新手,在將json消息傳遞給web api控制器時無法找到錯誤。我正在使用fiddler客戶端發佈複雜類型(模型對象)。我的模型始終爲空。它不是從json post對象讀取的。我究竟做錯了什麼?無法將json對象傳遞給asp.net web api中的控制器

我的模型:

public class LocationModel 
      { 


public int Customer {get; set;} 
    public string Name { get; set; } 
    public string AddressLine1 { get; set; } 
    public string AddressLine2 { get; set; } 
    public string Area { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string Country { get; set; } 


} 

我的控制器:

public class LocationController : ApiController 
    { 
[HttpPost] 
     public bool AddLocation([FromBody] LocationModel model) 
     { 
      MysqlRepository reps = new MysqlRepository(); 
      if (reps.LocationInsert(model)) 
       { 
        return true; 

       } 
       else 
       { 
        return false; 

       } 
      } 


    } 

的Json消息(使用招客戶端後):

var LocationModel = { 
Customer:9, 
      Name: "test", 
AddressLine1:"rrr", 
AddressLine2:"rrr", 
Area:"ddd", 
City:"ddd", 
State:"cooo", 
Country:"kkk" 
} 

$.ajax({ 
url: 'api/Location', 
type: 'POST', 
data: JSON.stringify(LocationModel), 
dataType: 'json', 
contentType: "application/json", 
success: function (data) { 

} 
}); 
+0

你得到什麼錯誤? –

回答

0

作爲新的提琴手的工具,我傳遞的頭信息的身體......這就是爲什麼輸入不從工具傳遞.... 正確的方法是通過fidd作爲:

{ 
"Customer":9, 
"Name": "test", 
"AddressLine1":"rrr", 
"AddressLine2":"rrr", 
"Area":"ddd", 
"City":"ddd", 
"State":"cooo", 
"Country":"kkk" 
} 
+0

感謝Saurabh Srivastava我修復了它 – MSGK

相關問題