I have the following error when I try to query my MVC 4 WebApi service
Extra content at the end of the document
Below is a rendering of the page up to the first error.
55Author Namehttp://images.myserver.com/authorspictures/nophoto.jpg
I looked into the Author class generated by Linq To Sql and found out that the next property of the Author object just after the image URL is this one:
[Association(Name = "Author_Quote", Storage = "_Quotes", ThisKey = "Id", OtherKey = "AuthorId")]
public EntitySet<Quote> Quotes { get; set; }
My undertanding is that there is some problem when generating the XML for this property causing the above error. However I don’t know how I can prevent that and make it working correctly. Most examples I found regarding webApi were using POCO lists so they can’t help me on this matter.
Any advices?
Below is my code
public class AuthorController : ApiController
{
IAuthorRepository repository;
public AuthorController(IAuthorRepository repository)
{
this.repository = repository;
}
[ResultLimit(20)]
public IQueryable<Author> GetAuthors(){
return repository.GetAuthors();
}
}
public class DBAuthorRepository : IAuthorRepository
{
public IQueryable<Author> GetAuthors()
{
using (var context = new QuotesDataContext())
{
return context.Authors.ToList().AsQueryable();
}
}
}
I found out that it was caused by deffered loading. So I added the following and now it works.