2013-04-04 56 views
3

我一直在为此进行了几天的研究。我有一个现有的MVC 4项目,它使用实体框架来创建数据库。该应用程序按预期工作,但我有一个新的要求,以添加web api到这个网站。这是一个引用数据库,并且要求返回仅包含完整数据库条目的有限信息的简单引用。将WebApi添加到使用实体框架的现有MVC4应用程序

我的原始模型:

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

上下文(*注被automagicly添加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对象,拿出我需要的信息,然后返回SimpleQuote对象?不知道该怎么做。有什么建议么?

回答

0

I 1+和接受@ CHammond的回应让我走上正轨。我确实需要返回一个SimpleQuote对象。这是我使用的代码。

  public SimpleQuote GetSimpleQuote(int id) 
    { 
     var quote = db.Quotes.Find(id); 
     if (quote == null) 
     { 
      throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); 
     } 

     SimpleQuote output = new SimpleQuote(); 
     output.Quote = quote.QName; 
     output.Author = quote.QAuthor; 
     output.Category = db.Categories.Find(quote.CategoryID).CatName; 

     return output; 

    } 
1

您提到了脚手架,您是否使用Code First创建数据库? 此外,您只希望SimpleQuote能够返回信息,它看起来像将其作为表格添加到数据库上下文中。当你真正想要的是从报价表中提取数据并构建或提取你想要的信息并返回。如果你不必返回一个SimpleQuote对象并且返回一个字符串,你可以写一些非常简单的东西。

public HttpResponseMessage GetSimpleQuote(int id) 
    { 
     var quote = db.Quotes.Find(id); 
     if (quote == null) 
     { 
      throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); 
     } 

     var output += "ID: " + quote.Id + " Quote: " + quote.Quote + " Author: " + quote.Author + " Category: " + quote.Category ; 

     var response = new HttpResponseMessage(); 

     response.Content = new StringContent(output); 
     response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); 

     return response; 

    } 
+0

@jbolt:不要对显着改变原始上下文的答案进行编辑。 – 2013-04-04 12:26:14

相关问题