I have api controller that returns one object that contain two lists of objects.
I expect that I will get this result when I call this api using ajax:
one object that contain a long value and two lists of objects:
{
"userId":26000000000000228,
"paidInvoices":
[{"id":13060000000000039,"total":10},
{"id":13060000000000040,"total":20},
{"id":13060000000000041,"total":30}],
"unPaidInvoices":
[{"id":15060000000000039,"total":10},
{"id":15060000000000040,"total":20},
{"id":15060000000000041,"total":30}]
}
but I get unexpected repeated ids (all end with 40) :
{
"userId":26000000000000228,
"paidInvoices":
[{"id":13060000000000040,"total":10},
{"id":13060000000000040,"total":20},
{"id":13060000000000040,"total":30}],
"unPaidInvoices":
[{"id":15060000000000040,"total":10},
{"id":15060000000000040,"total":20},
{"id":15060000000000040,"total":30}]
}
Code:
public class Invoice
{
public long Id { get; set; }
public decimal Total { get; set; }
}
public class Invoices
{
public long UserId { get; set; }
public List<Invoice> PaidInvoices { get; set; }
public List<Invoice> UnPaidInvoices { get; set; }
}
[Route("NetApi/[controller]")]
[ApiController]
public class PurchasePaymentController : ControllerBase
{
[HttpGet("Search/{id}")]
public Task<Invoices> Search(long id)
{
var paid = new List<Invoice>
{
new Invoice {Id = 13060000000000039, Total = 10},
new Invoice {Id = 13060000000000040, Total = 20},
new Invoice {Id = 13060000000000041, Total = 30}
};
var unPaid = new List<Invoice>
{
new Invoice {Id = 15060000000000039, Total = 10},
new Invoice {Id = 15060000000000040, Total = 20},
new Invoice {Id = 15060000000000041, Total = 30}
};
return Task.FromResult(new Invoices {UserId = id, PaidInvoices = paid, UnPaidInvoices = unPaid});
}
}
I've tried to return JsonResult,it doesn't make any difference.
and I call it from this razorPage which doesn't use any layout:
@page
@model MyWebSite.Pages.Test.IndexModel
@{
Layout = null;
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h1>Welcome</h1>
<div id="result"></div>
<script>
// if I use $.ajax or $.get , the result is the same (weird/unexpected result)
$(function () {
$.ajax({
type: "GET",
url: "@Url.RouteUrl("NetApi", new {controller = "PurchasePayment", action = "Search", id = 26000000000000228})",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: getCallback
});
});
$(function() {
debugger;
$.get("@Url.RouteUrl("NetApi", new {controller = "PurchasePayment", action = "Search", id = 26000000000000228})",
getCallback, "json");
});
function getCallback(response) {
console.log(JSON.stringify(response));
debugger;
}
// but this load method works fine
$(function() {
$("#result").load("@Url.RouteUrl("NetApi", new {controller = "PurchasePayment", action = "Search", id = 26000000000000228})");
});
</script>
</body>
</html>
in my Startup.cs
// ConfigureServices
services.AddMvc().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore
);
// Configure
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "api",
pattern: "api/{controller}/{action}/{id?}",
defaults: new { controller = "login", action = "Index" });
endpoints.MapControllerRoute(
name: "NetApi",
pattern: "NetApi/{controller}/{action}/{id?}");
endpoints.MapRazorPages();
});