0

MVC 4 WebApi サービスを照会しようとすると、次のエラーが発生します。

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

Linq To Sql によって生成された Author クラスを調べたところ、画像 URL の直後にある Author オブジェクトの次のプロパティが次のプロパティであることがわかりました。

    [Association(Name = "Author_Quote", Storage = "_Quotes", ThisKey = "Id", OtherKey = "AuthorId")]
    public EntitySet<Quote> Quotes { get; set; }

私の理解では、このプロパティの XML を生成するときに問題が発生し、上記のエラーが発生します。ただし、それを防ぎ、正しく機能させる方法がわかりません。webApi に関して私が見つけたほとんどの例は POCO リストを使用していたため、この問題については役に立ちません。

何かアドバイスはありますか?

以下は私のコードです

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

2 に答える 2

3

遅延読み込みが原因であることがわかりました。だから私は以下を追加し、今では動作します。

public class DBAuthorRepository : IAuthorRepository
{
    public IQueryable<Author> GetAuthors()
    {
        using (var context = new QuotesDataContext())
        {  
            context.DeferredLoadingEnabled = false; // Added this line
            return context.Authors.ToList().AsQueryable();
        }
    }
}
于 2012-05-17T13:16:41.643 に答える
0

@alexandrekowの回答から、mvc4 webapiで上記のエラーが発生しました。解決策を見つけました。

 entity = new BrewedlifeEntities();
 entity.ContextOptions.ProxyCreationEnabled = false; // Added this line
于 2012-10-26T15:02:20.647 に答える