Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding WebApi to an existing MVC4 app that uses Entity framework
    primarykey
    data
    text
    <p>I've been going around with this for a few days now. I have an existing MVC 4 project that uses entity framework for database creation. The app works as intended but I have a new requirement to add web api to this site. This is a database of quotes and the requirement is to return a simple quote that only contains limited information of the full database entry.</p> <p>My original model:</p> <pre><code> 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; } } } </code></pre> <p>The Simple Quote Model</p> <pre><code> 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; } } } </code></pre> <p>The Context (*Note the SimpleQuote entry was added automagicly when I scaffold the new QuoteApiController)</p> <pre><code> 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&lt;Quote&gt; Quotes { get; set; } public DbSet&lt;Category&gt; Categories { get; set; } public DbSet&lt;UserProfile&gt; UserIds { get; set; } public QuoteContext() { Configuration.ProxyCreationEnabled = false; } public DbSet&lt;SimpleQuote&gt; SimpleQuotes { get; set; } } } </code></pre> <p>This returns an error when accessing the /api/quoteapi/ page with </p> <p>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.</p> <p>Obviously this error occurs because it is trying to return a SimpleQuote Object that doesn't exist in the database.</p> <p>The API Controller that was created.</p> <pre><code> 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&lt;SimpleQuote&gt; 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); } } } </code></pre> <p>Where am I going awry. I can't return a model that doesn't exist in the database. If I change the call in the api to return a Quote model that works but I only need to return the quote, author and category as strings. Would I just return the Quote object in the controller, pull out the information I need and then return the SimpleQuote object? Not sure how to do that. Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload