1

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?

2
  • Sure you have correct value in id? Commented Feb 26, 2014 at 9:40
  • Yes, the value is correct, and the item is in the database. Commented Feb 26, 2014 at 9:45

1 Answer 1

1

Uhm, I'm almost afraid to tell what the reason for this is...

The two controllers exist in each of their own web projects, and the project with the MVC controller was missing the connectionstring in web.config... DUH...

Think I need some sleep.....

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.