【发布时间】:2012-06-05 12:42:41
【问题描述】:
昨天经过大量帮助后,我在 asp.net4 beta 中遇到了一个已知错误 - 我升级到 VS2012 RC Express (4.5),现在我收到内部服务器错误,我看不到为什么。我正在创建一个 Web API:
型号
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
namespace MvcApplication6.Models
{
public class tblCustomerBooking
{
[Key()]
public int customer_id { get; set; }
public string customer_name { get; set; }
public string customer_email { get; set; }
public virtual ICollection<tblRental> tblRentals { get; set; }
}
public class tblRental
{
[Key()]
public int rental_id { get; set; }
public int room_id { get; set; }
public DateTime check_in { get; set; }
public DateTime check_out { get; set; }
public decimal room_cost { get; set; }
public int customer_id { get; set; }
[ForeignKey("customer_id")]
public virtual tblCustomerBooking tblCustomerBooking { get; set; }
}
}
然后我使用添加控制器向导,选择“模板:具有读/写动作的 API 控制器,使用实体框架”,选择 tblCustomerBooking 作为我的模型类,然后单击,即:
using System.Data.Entity;
namespace MvcApplication6.Models
{
public class BookingsContext : DbContext
{
public BookingsContext() : base("name=BookingsContext")
{
}
public DbSet<tblCustomerBooking> tblCustomerBookings { get; set; }
}
}
Visual Studio 2012 Express 自动生成的我的控制器(BookingsController.cs)是:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using MvcApplication6.Models;
namespace MvcApplication6.Controllers
{
public class BookingsController : ApiController
{
private BookingsContext db = new BookingsContext();
// GET api/Bookings
public IEnumerable<tblCustomerBooking> GettblCustomerBookings()
{
return db.tblCustomerBookings.AsEnumerable();
}
}
}
我在上面的“return db.....”处添加了一个断点,并检查了 VS 中的 Watch 部分 - 它清楚地显示了对象、客户以及相关的租金:
但是,如果我允许脚本继续运行,我只会收到 http500 错误(如下面的 Fiddler 所示):
是否有更多代码可以添加到控制器中,让我了解它为什么出错?或者任何人都可以看到可能出了什么问题? VS 似乎可以找回它,如第一张截图所示,但似乎无法将其发送出去。
感谢任何帮助或指点,
标记
更新
您好 - 我是否只是对 API 提出了太多要求?它不可能(开箱即用)简单地返回具有一对多关系的对象吗?真的只能产生一个对象列表吗?
谢谢,马克
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-web-api