我创建了一个简单的API控制器,并在其中添加了以下方法:
public IQueryable<tbl_Book> GetBooks()
{
//AspNetUser user = new AspNetUser();
return _context.tbl_Book;
}
然后我创建了一个MVC控制器来使用这个API,如下所示:
public ActionResult Index()
{
IEnumerable<tbl_Book> books = null;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:57368/api/");
var responseTask = client.GetAsync("Admin");
responseTask.Wait();
var result = responseTask.Result;
if(result.IsSuccessStatusCode)
{
var readTask = result.Content.ReadAsAsync<IEnumerable<tbl_Book>>();
readTask.Wait();
books = readTask.Result;
}
else
{
ModelState.AddModelError(string.Empty, "Error occured while fetching data from api");
}
}
return View(books);
}
它工作正常并给出结果但我想从两个表中获取数据,这些表是tbl_Categories和AspNetusers,我喜欢这样:
public IQueryable<tbl_Book> GetBooks()
{
//AspNetUser user = new AspNetUser();
return _context.tbl_Book.Include(x=>x.AspNetUser).Include(x=>x.tbl_Category);
}
书模型有来自tbl_Categories和AspNetUsers的外键,但是当我尝试使用包含函数的上述API时,它会给出null结果。