加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.Net Web API在VS中正确显示但提供HTTP500

发布时间:2020-12-16 00:01:24 所属栏目:asp.Net 来源:网络整理
导读:经过昨天的大量帮助后,我遇到了asp.net4测试版中的已知错误 – 我升级到VS2012 RC Express(4.5),现在我收到内部服务器错误,我看不出原因.我正在创建一个Web API: 模型 using System;using System.Collections.Generic;using System.Linq;using System.Web;u
经过昨天的大量帮助后,我遇到了asp.net4测试版中的已知错误 – 我升级到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; }
    }
}

然后我使用Add Controller向导,选择“Template:API controller with read / write actoins,using Entity Framework”,选择tblCustomerBooking作为我的Model Class,然后单击,即:

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();
        }
    }
}

我在上面的“返回db …..”处添加了一个断点,并检查了VS中的Watch部分 – 它清楚地显示了对象,与客户以及相关的租赁:

但是,如果我允许脚本继续,我只会得到一个http500错误(如下面的Fiddler所示):

是否还有我可以添加到控制器中的代码,以便让我了解它为什么会出错?或者任何人都可以看到可能出错的地方? VS似乎可以检索它,如第一个屏幕截图所示,但似乎无法将其发送出去.

感谢您的帮助或指示,

标记

更新

嗨 – 我只是问过多的API?是不是(开箱即用)它只能返回具有一对多关系的对象?它只能真正生成一个对象列表吗?

谢谢,马克

解决方法

你在做什么:
db.tblCustomerBookings.Include("tblRentals").Select(i => 
    new { i.something //etc });

还有,您使用的是哪种MediaTypeFormatter,Xml还是Json?错误500通常意味着Formatter正在窒息.

切换到JSON.NET格式化程序(在Web API RC中最简单的方法是执行GlobalConfiguration.Configuration.Formatters.RemoveAt(1) – 这将删除XML格式化程序)并查看它是否有帮助或至少提供更有意义的错误(或请求您的方法内容类型为JSON).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读