Asp.net mvc 4 Web API获取Http 500错误

Asp.net mvc 4 Web API获取Http 500错误,asp.net-mvc-4,asp.net-web-api,Asp.net Mvc 4,Asp.net Web Api,这是我的MVC控制器,一切正常: private UnitOfWork UOW; public InventoryController() { UOW = new UnitOfWork(); } // GET: /Inventory/ public ActionResult Index() { var products = UOW.ProductRepository.GetAll().ToList();

这是我的MVC控制器,一切正常:

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

    // GET: /Inventory/
    public ActionResult Index()
    {
        var products = UOW.ProductRepository.GetAll().ToList();
        return View(products);
    }
API控制器中的相同方法调用给我一个Http 500错误:

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

    public IEnumerable<Product> Get()
    {
        var products = _unitOfWork.ProductRepository.GetAll().ToList();
        return products;
    }
我仍然只在API控制器中收到Http 500,不知为什么。有什么想法吗

更新:


似乎是使用延迟加载导致了问题。当我将相关属性设置为非虚拟时,测试API提供了必要的JSON字符串。然而,在包含Vendor类之前,我只有VendorId。我真的很想包括相关的类。有什么想法吗?我知道外面有很多聪明人。有人吗?

问题已解决:

问题毕竟不是延迟加载。问题是,虽然我在产品中正确地关联了供应商,但我在供应商中也有一个产品集合,这可能会导致一些循环:

public class Product 
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int VendorId { get; set; }
    public virtual Vendor Vendor { get; set; }
}

public class Vendor 
{
    public int Id { get; set; }
    //public Vendor()
    //{
    //    Products = new List<Product>();
    //}

    public string CompanyName { get; set; }
  //  public ICollection<Product> Products { get; set; }
}

此500响应是否包含任何有关异常的详细信息(大多数情况下是这样的)?Lucky me[讽刺]“网站在检索时遇到错误。它可能因维护而关闭或配置不正确。”@JoeGrasso-最好将您的答案标记为答案,并从问题标题中删除“已解决”。仅供参考。
public class Product 
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int VendorId { get; set; }
    public virtual Vendor Vendor { get; set; }
}

public class Vendor 
{
    public int Id { get; set; }
    //public Vendor()
    //{
    //    Products = new List<Product>();
    //}

    public string CompanyName { get; set; }
  //  public ICollection<Product> Products { get; set; }
}
                    "vendor": {
                        "id": 4,
                        "companyName": "Vendor 3"
                    },
                    "id": 1,
                    "name": "Product 1",
                    "vendorId": 4
                },
                {
                    "vendor": {
                        "id": 2,
                        "companyName": "Vendor 4"
                    },
                    "id": 2,
                    "name": "Product 2",
                    "vendorId": 2
                },
                {
                    "vendor": {
                        "id": 3,
                        "companyName": "Vendor 2"
                    },
                    "id": 3,
                    "name": "Product 3",
                    "vendorId": 3
                },
                {
                    "vendor": {
                        "id": 1,
                        "companyName": "Vendor 1"
                    },
                    "id": 4,
                    "name": "Product 4",
                    "vendorId": 1
                }]