Asp.net 如何在web API get by id函数中根据id返回所有记录
下面是我的get by id函数,它返回用户帐户表中与给定城镇id匹配的第一行或默认行,我想返回具有给定城镇id的所有行(用户)Asp.net 如何在web API get by id函数中根据id返回所有记录,asp.net,Asp.net,下面是我的get by id函数,它返回用户帐户表中与给定城镇id匹配的第一行或默认行,我想返回具有给定城镇id的所有行(用户) public HttpResponseMessage Get(string id) { using(KWSB_dbEntities entities = new KWSB_dbEntities()) { entities.Configuration.ProxyCreationEnabled = false; var entity
public HttpResponseMessage Get(string id) {
using(KWSB_dbEntities entities = new KWSB_dbEntities()) {
entities.Configuration.ProxyCreationEnabled = false;
var entity = entities.User_Account.FirstOrDefault(e = > e.Town_Id == id);
if (entity != null) {
return Request.CreateResponse(HttpStatusCode.OK, entity);
} else {
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User_Account with Town id = " + id.ToString() + " not found ");
}
}
}
这不只是:
var entity = entities.User_Account.FirstOrDefault(e = > e.Town_Id == id);
需要成为
var entity = entities.User_Account.Where(e = > e.Town_Id == id);
不同之处在于where子句选择所有匹配行,firstordefault只选择第一个匹配行
public HttpResponseMessage Get(string id) {
using(KWSB_dbEntities entities = new KWSB_dbEntities()) {
entities.Configuration.ProxyCreationEnabled = false;
entities.Configuration.LazyLoadingEnabled = false;
var entity = entities.User_Account.FirstOrDefault(e = > e.Town_Id == id);
if (entity != null) {
return Request.CreateResponse(HttpStatusCode.OK, entity);
} else {
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User_Account with Town id = " + id.ToString() + " not found ");
}
}
}我尝试了上述建议,但在浏览器上出现了此错误。“$id:”1“,”消息“:”发生错误“,”异常消息“:”ObjectContent“1”类型未能序列化内容类型“application/json;charset=utf-8”的响应正文。”