2016-09-28 78 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

相关问题