0

In my ProductController, I have this code to return Json:

[HttpGet]
public IActionResult GetAllProduct()
{
    List<Product> objProductList = _unitOfWork.ProductRepository
                                              .GetAll(includeProperties: "Category")
                                              .ToList();

    // return Json(new { data = objProductList });
    return Json(objProductList);
}        

And when I run the web and route to action, I get this error - please help:

Screenshot

Code has been upload here for investigation https://github.com/nguyentuananh921/Bulky_MVC/commits/master

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nguyen Tuan Anh
  • 29
  • 1
  • 1
  • 7
  • 1
    It would be great if you could share your ERROR code instead of screenshot. Post the error as text, not images. Images can't be copied, googled or tested. – Qing Guo Jul 19 '23 at 02:34
  • Do you set a breakpoint at `List objProductList` ? Can you get the value ? – Qing Guo Jul 19 '23 at 03:29
  • 1
    `Bulky_MVC/commits/master` - is this a private repo, because we can't see a private one. And I can't see this one, it's 404 for me. – Guru Stron Jul 19 '23 at 06:16
  • Sorry sir, I have made it to Public, please try it. I have set the breakpoint at this List objProductList And I can see the value. Thanks for your help. – Nguyen Tuan Anh Jul 19 '23 at 16:26

1 Answers1

1

Such errors usually are a sign of some package mismatch. Run in command line: dotnet --list-sdks and check that output contains latest .NET 8 preview (currently it is 8.0.100-preview.6.23330.14, if not - install it). Also run dotnet --version via command line in the project folder and make sure correct version is shown.

Also delete the following package from the Models project:

<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0" />

And replace it with framework reference (see the Use the ASP.NET Core shared framework doc):

<ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

P.S.

Commented out .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()); because it makes project uncompilable. Probably you have meant:

.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())

Which requires Microsoft.AspNetCore.Mvc.NewtonsoftJson package installed.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Great, Thanks it work. – Nguyen Tuan Anh Jul 20 '23 at 00:26
  • @NguyenTuanAnh was glad to help. If answer works for you - feel free to mark it as accepted one (checkmark on the left) – Guru Stron Jul 20 '23 at 02:28
  • I have mark it as a answer. And One thing that I wondering that. I follow the video here https://www.youtube.com/watch?v=AopeJjkcRvU To learn. And I can't see the difference between my step but the their project works but mine doesn't work. – Nguyen Tuan Anh Jul 20 '23 at 04:44
  • @NguyenTuanAnh no, you have upvoted it. To mark it as an answer you can click on checkmark outline to the left of it. As for the video - I will try to look at it but you need to explain what exactly does not work. – Guru Stron Jul 20 '23 at 05:21