ASP.Net的Web API显示正确VS但给人HTTP500 [英] ASP.Net Web API showing correctly in VS but giving HTTP500

查看:104
本文介绍了ASP.Net的Web API显示正确VS但给人HTTP500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多的帮助后,昨天,我想出了反对ASP.NET4测试已知的错误 - 我升级到VS2012 RC防爆preSS(4.5),现在我得到一个内部服务器错误,我不明白为什么。我创建一个Web API:

after a lot of help yesterday, I came up against a known error in asp.net4 beta - I upgraded to VS2012 RC Express (4.5), and now I'm getting an internal server error, and I can't see why. I'm creating a 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控制器/使用实体框架写actoins,选择tblCustomerBooking作为我的模型类,并点击,这是:

I then used the Add Controller wizard, selected "Template: API controller with read/write actoins, using Entity Framework", chose tblCustomerBooking as my Model Class, and clicked , which is:

using System.Data.Entity;

namespace MvcApplication6.Models
{
    public class BookingsContext : DbContext
    {
        public BookingsContext() : base("name=BookingsContext")
        {
        }
        public DbSet<tblCustomerBooking> tblCustomerBookings { get; set; }
    }
}

我的控制器(BookingsController.cs)由Visual Studio自动生成的2012例preSS是:

My Controller (BookingsController.cs) automatically generated by Visual Studio 2012 Express is:

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

我在加断点回归分贝.....上面,并检查在VS监视一部分 - 它清楚地表明了对象,与客户,以及相关的租金:

I added a breakpoint at "return db....." above, and checked the Watch part in VS - it clearly shows the object, with the customer, and the associated rentals:

但是,如果我允许脚本继续,我刚刚得到一个HTTP500错误(如下面小提琴手):

However if I allow the script to continue, I just get an http500 error (as shown in Fiddler below):

有没有更多的code,我可以加入到控制器,让我看到它为什么示数?或者,任何人都可以看到什么可能是错的? VS似乎找回它确定,如图中第一张截图,但似乎并没有能够发送出去。

Is there any more code I can add into the controller to allow me to see why it is erroring? Or can anyone see what may be wrong? VS appears to retrieve it ok, as shown in the first screenshot, but doesn't seem to be able to send it out.

感谢您的帮助或指针,

标记

更新

喜 - 我,我只是要求太多的API?它是不可能的(开箱即用),它简单地与一对多的关系返回的对象?可以把它只有真正产生一个单一的对象列表?

Hi - am I simply asking too much of the API? Is it not possible (out of the box) for it to simply return objects with one to many relationships? Can it only really produce a single object list?

谢谢,马克

推荐答案

您这样做的:

db.tblCustomerBookings.Include("tblRentals").Select(i => 
    new { i.something //etc });

此外,这MediaTypeFormatter您使用的是XML或JSON?错误500往往意味着格式化窒息。

also, which MediaTypeFormatter are you using, Xml or Json? Error 500 often means that Formatter is choking.

切换到JSON.NET格式(在网页API RC最简单的方法就是做 GlobalConfiguration.Configuration.Formatters.RemoveAt(1) - 这将删除XML格式),并看看是否有帮助,或者至少给出更有意义的错误(或请求的内容类型的JSON你的方法)。

Switch to JSON.NET formatter (in Web API RC the easiest way is to do GlobalConfiguration.Configuration.Formatters.RemoveAt(1) - this removes XML formatter) and see if it helps or at least gives more meaningful error (or request your method with content type JSON).

这篇关于ASP.Net的Web API显示正确VS但给人HTTP500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆