Never saw this before. I use both web api and mvc controllers in my app.
I use Structuremap MVC4 dependency injection for both controllers, which works fine.
Using the same repository, when I fetch a procuct from the MVC controller, the result is NULL, but calling for the same product from the Web Api controller, the result is the correct product.
Both hit the same method in the repository, using the same DbContext.
This is the MVC controller action:
public ActionResult ShowProduct(int id)
{
var product = _repo.GetProduct(id);//product is null
return View();
}
This is the Web Api method:
public Product Get(int id)
{
var product = _repo.GetProduct(id);//Returns correct product
return product;
}
This is the repository method used by both controllers:
public Product GetProduct(int id)
{
using (var db = new ProductContext())
{
var product = db.ProductDb.Include(a => a.Orders).FirstOrDefault(c => c.Id == id);
return product; //Call from MVC controller returns NULL
}
}
Any ideas why this happens?