![](/img/trans.png)
[英]WebAPI issue while retrieving data from database using Entity Framework
[英]Get a table data from database using 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 的控制器(具有讀/寫操作的 Web API 2 控制器)。
然后我添加了這個代碼來添加和檢索數據庫中的數據。 但不工作。
CodeXenETSEntities db = new CodeXenETSEntities();
[HttpPost]
[ActionName("pushlocation")]
// POST: api/pushlocation
public HttpResponseMessage pushlocation( int userid,decimal longitude, decimal latitude , TimeSpan time )
{
user_locations ulog = new user_locations();
ulog.user_id = userid;
ulog.lon = longitude;
ulog.lat = latitude;
ulog.time = time;
db.user_locations.Add(ulog);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.Accepted, "Successfully Created");
}
[HttpGet]
[ActionName("pulllocation")]
// GET: api/pulllocation/5
public HttpResponseMessage pulllocation(int userid, decimal longitude, decimal latitude, TimeSpan time)
{
db.user_locations.ToList();
return Request.CreateResponse(HttpStatusCode.Accepted, "Success");
}
數據庫數據:
3件事:
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
我希望這會解決
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.