这几天我一直在处理这个问题。我有一个现有的 MVC 4 项目,它使用实体框架来创建数据库。该应用程序按预期工作,但我有一个新要求将 Web api 添加到该站点。这是一个报价数据库,要求返回一个仅包含完整数据库条目的有限信息的简单报价。
我的原始模型:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    namespace Exercise4.Models
    {
        public class Quote
        {
            public int QuoteID { get; set; }
            [Required (ErrorMessage = "A Category Field Must be selected or a New one must be Created before continuing")]
            public int CategoryID { get; set; }
            [Display (Name="Quotation")]
            [Required (ErrorMessage = "A Quotation is Required")]
            public string QName { get; set; }
            [Display (Name= "Author")]
            [Required (ErrorMessage = "An Authors Name is Required\n Use 'Unknown' if needed")]
            public string QAuthor { get; set; }
             [Display (Name = "Category")]
             public virtual Category category { get; set; }
             [DisplayFormat(DataFormatString = "{0:d}")]
             public DateTime Date { get; set; }
             public int UserId { get; set; }      
             }
         }
简单报价模型
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    namespace Exercise4.Models
    {
        public class SimpleQuote
        {
            public int Id { get; set; }
            public string Quote { get; set; }
            public string Author { get; set; }
            public  string Category { get; set; }
         }
     }
上下文(*注意 SimpleQuote 条目是在我搭建新的 QuoteApiController 时自动添加的)
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    namespace Exercise4.Models
    {
        public class QuoteContext : DbContext
        {
            public DbSet<Quote> Quotes { get; set; }
            public DbSet<Category> Categories { get; set; }
            public DbSet<UserProfile> UserIds { get; set; }
            public QuoteContext()
            {
                Configuration.ProxyCreationEnabled = false;
            }
            public DbSet<SimpleQuote> SimpleQuotes { get; set; }
        }
    }
这在访问 /api/quoteapi/ 页面时返回错误
“ObjectContent`1”类型无法序列化内容类型“application/xml”的响应正文;字符集=utf-8'。
显然,发生此错误是因为它试图返回数据库中不存在的 SimpleQuote 对象。
创建的 API 控制器。
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web;
    using System.Web.Http;
    using Exercise4.Models;
    namespace Exercise4.Controllers
    {
        public class QuoteApiController : ApiController
        {
            private QuoteContext db = new QuoteContext();
            // GET api/QuoteApi
            public IEnumerable<SimpleQuote> GetSimpleQuotes()
            {   
                return db.SimpleQuotes.AsEnumerable();
            }
            // GET api/QuoteApi/5
            public SimpleQuote GetSimpleQuote(int id)
            {
                SimpleQuote simplequote = db.SimpleQuotes.Find(id);
                if (simplequote == null)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
                }
                return simplequote;
            }
            protected override void Dispose(bool disposing)
            {
                db.Dispose();
                base.Dispose(disposing);
            }
        }
    }
我哪里出错了。我无法返回数据库中不存在的模型。如果我更改 api 中的调用以返回有效的 Quote 模型,但我只需要将引用、作者和类别作为字符串返回。我会在控制器中返回 Quote 对象,提取我需要的信息,然后返回 SimpleQuote 对象吗?不知道该怎么做。有什么建议么?