2016-09-15 88 views
1

我从数据库表中创建了一个实体数据模型就像这样。 enter image description here使用WebApi&Entity Framework从数据库获取表数据6.XX

然后在WebApiConfig.cs中添加4行代码以从响应中获取json数据。

var json = config.Formatters.JsonFormatter; 
     json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; 
     json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
     config.Formatters.Remove(config.Formatters.XmlFormatter); 

然后,我创建了一个名为importController的Controller(带有读/写操作的Web API 2控制器)。

然后我添加了这些代码来添加和检索数据库中的数据。但不工作。

​​

这是输出: enter image description here

数据库数据:

enter image description here

+0

我相信你的网址应沿HTTP线的东西://本地主机:57715/API /进口/ pulllocation/ –

+0

此外,你的路线配置在'WebApiConfig .cs'可能不允许无参数调用,因为您已在'pulllocation'方法中指定了四个参数 – sly

+0

@AndyArndt感谢您的建议。但仍然无法正常工作。任何其他想法或建议? –

回答

1

三件事:

1)WebApiConfig.cs使用routeTemplate: "api/{controller}/{action}/{id}"

2)importController变化public HttpResponseMessage pulllocation(int userid, decimal longitude, decimal latitude, TimeSpan time)public HttpResponseMessage pulllocation()

3)在浏览器地址栏中使用http://localhost:57715/api/{controller name}/pulllocation。你的情况http://localhost:57715/api/import/pulllocation

我希望这将解决

+0

谢谢。你救了我:D –

相关问题