C
C#2y ago
K0unna

✅ I get Internal Server Error when uses Include

[HttpGet]
public async Task<ICollection<Product>> Get()
{
var reviews = await _context.reviews.ToListAsync();
var products = await _context.products.ToListAsync();
List<ProductReviewModel> PRML = new List<ProductReviewModel>();
foreach (var i in products)
{
ProductReviewModel PRM = new ProductReviewModel()
{
Product = i
};

PRM.Review = reviews.FindAll(x => x.ProductId == i.Id);

PRML.Add(PRM);
}
return products;
}
[HttpGet]
public async Task<ICollection<Product>> Get()
{
var reviews = await _context.reviews.ToListAsync();
var products = await _context.products.ToListAsync();
List<ProductReviewModel> PRML = new List<ProductReviewModel>();
foreach (var i in products)
{
ProductReviewModel PRM = new ProductReviewModel()
{
Product = i
};

PRM.Review = reviews.FindAll(x => x.ProductId == i.Id);

PRML.Add(PRM);
}
return products;
}
In this code I get InternalServerError. Also if I use Include instead of whole this code, then I get InternalServerError again
7 Replies
K0unna
K0unnaOP2y ago
But if I comment all lines except 5th line (car product ...) then code works perfectly
Saber
Saber2y ago
the actual exception would be useful.
K0unna
K0unnaOP2y ago
actual exception?
Jimmacle
Jimmacle2y ago
yes, your code is throwing an exception and it causes your API to return a 500 500 is just the error shown to the client, something more specific is going wrong
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
K0unna
K0unnaOP2y ago
services.AddControllersWithViews().AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore ); I added this code and it works now Thanks for your time
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server