Web API Getting Http 500 error : Issue Solved See Below

Posted by Joe Grasso on Stack Overflow See other posts from Stack Overflow or by Joe Grasso
Published on 2012-09-28T14:12:11Z Indexed on 2012/09/28 15:37 UTC
Read the original article Hit count: 196

Here is my MVC Controller and everything is fine:

    private UnitOfWork UOW;
    public InventoryController()
    {
        UOW = new UnitOfWork();
    }

    // GET: /Inventory/
    public ActionResult Index()
    {
        var products = UOW.ProductRepository.GetAll().ToList();
        return View(products);
    }

Same method call in API Controller gives me an Http 500 Error:

    private UnitOfWork _unitOfWork;
    public TestController()
    {
        _unitOfWork = new UnitOfWork();
    }

    public IEnumerable<Product> Get()
    {
        var products = _unitOfWork.ProductRepository.GetAll().ToList();
        return products;
    }

Debugging shows that indeed there is data being returned in both controllers' UOW calls. I then added a customer configuration in Global:

    public static void CustomizeConfig(HttpConfiguration config)
    {
        config.Formatters.Remove(config.Formatters.XmlFormatter);
        var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    }

I am still receiving an Http 500 in API Controller ONLY and at a loss as to why. Any ideas?

UPDATE:

It appears using lazy loading caused the problem. When I set the associated properties to NON-VIRTUAL the Test API provided the necessary JSON string. However, whereas before I had the Vendor class included, I only have VendorId. I really wanted to included the associated classes. Any ideas? I know there are alot of smart people out there. Anyone?

© Stack Overflow or respective owner

Related posts about asp.net-mvc-4

Related posts about asp.net-web-api