这是我的表:
表名:数据
字段:
- Id
- 类别
- description
- 的ImagePath
我正在构建一个webApi,它返回所有imagePath或id。
web api控制器:
namespace task.Controllers
{
public class ImagesController : ApiController
{
DataEntry db = new DataEntry();
public IEnumerable<datum> GetImages()
{
var imagePath = from m in db.data select m;
return imagePath;
}
public IHttpActionResult GetImages(int id)
{
var imagePath = from m in db.data select m;
var imagPath = imagePath.Where((p) => p.Id == id);
if (imagePath == null)
{
return NotFound();
}
return Ok(imagePath);
}
}
}
它返回所有字段(Id,category,description和imagePath)而不是仅返回imagePath。对于select by Id方法,它也不起作用,所以出了什么问题?
答案 0 :(得分:0)
对于imagePath
,请尝试在linq查询中指定它,如下所示:
public IEnumerable<string> GetImages()
{
var imagePath = from m in db.data select m.imagePath;
return imagePath.ToList();
}
对于select by id,请尝试:
public string GetImages(int id)
{
var imagePath = from m in db.data select m;
var imagPath = imagePath.Where((p) => p.Id == id);
if (imagPath == null)
{
return NotFound();
}
return Ok(imagPath.imagePath);
}
仔细检查错误,例如imagPath
和imagePath