gpt4 book ai didi

ASP.Net Web API 在 VS 中正确显示但给出 HTTP 500

转载 作者:行者123 更新时间:2023-12-05 00:34:17 24 4
gpt4 key购买 nike

经过昨天的大量帮助,我遇到了 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; }
}
}
然后我使用添加 Controller 向导,选择“模板:具有读/写 Action 的 API Controller ,使用 Entity Framework ”,选择 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 自动生成的我的 Controller (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 部分 - 它清楚地显示了对象、客户以及相关的租金:
Snapshot of customer and rental objects
但是,如果我允许脚本继续运行,我只会收到一个 http500 错误(如下面的 Fiddler 所示):
Fiddler screenshot showing HTTP500
是否还有更多代码可以添加到 Controller 中以让我了解它为什么出错?或者任何人都可以看到可能有什么问题? VS 似乎可以正常检索它,如第一个屏幕截图所示,但似乎无法将其发送出去。
感谢您的任何帮助或指示,
标记
更新
嗨 - 我只是对 API 要求太多了吗?它是否不可能(开箱即用)简单地返回具有一对多关系的对象?它真的只能生成一个对象列表吗?
谢谢,马克

最佳答案

为了解决返回带有 virtual 关键字的实体的 JSON 时出现的错误 500 问题,我执行了以下操作,

    public class BookingsController : ApiController
{
private BookingsContext db = new BookingsContext();

// GET api/Bookings
public IEnumerable<tblCustomerBooking> GettblCustomerBookings()
{
db.Configuration.ProxyCreationEnabled = false;
return db.tblCustomerBookings.AsEnumerable();
}
}

对于代理令人不安的特定情况(例如序列化),禁用代理创建(也禁用延迟加载)就足够了。这仅针对 db 的特定上下文实例禁用代理创建。
db.Configuration.ProxyCreationEnabled = false;

http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx

http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-1-introduction-and-model.aspx

关于ASP.Net Web API 在 VS 中正确显示但给出 HTTP 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10897523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com