I added this line to resolve circular errors in my JSON:
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
But now, I find that some of my controllers return too much information.
I found this similiar question, but reading through it, I couldn't really figure out how to apply it to my code: Avoiding Circular referencing providing too much data
For example, this simple controller returns data that I don't want:
[HttpGet("{id}")]
public async Task<ActionResult<BookList>> GetBookList(string id)
{
var bookList = await _context.BookList.FindAsync(id);
return bookList;
}
Here is the model for that data:
public partial class BookList
{
public BookList()
{
BookLinks = new HashSet<BookLinks>();
}
public string BookId { get; set; }
public Guid LibraryId { get; set; }
public string BookTitle { get; set; }
public string BookText { get; set; }
public byte? BookType { get; set; }
public virtual LibraryList Library { get; set; }
public virtual ICollection<BookLinks> BookLinks { get; set; }
}
}
So when I hit the controller above, I am getting all the unwanted data for BookLinks in addition to the data from BookList.
I just want the data from BookList based on a particular BookId.
I was under the impression, that if I wanted all data returned(including the BookLinks data), I'd have to do something like this:
var bookList = await _context.BookList
.Include(i => i.BookLinks)
.Where(b => b.BookId == id)
.ToListAsync();
That said, is there way to limit or exclude data that I don't want?
Thanks!