Asp.Net Core Razor Pages Failed AJAX request after migrating to Version 3.1

AJAX requests are no longer working after migrating from .NET Core 2.1 to 3.1.

Previous Code

AJAX call

$.ajax({     type: "POST",     url: "/Controller/ControllerA",     data: JSON.stringify({         Name: name,         Value: value     }),     contentType: 'application/json; charset=utf-8',     dataType: "json",     success: { ...}, }); 

Model

public class ItemModel {     public string Name { get; set; }     public string Value { get; set; } } 

Action

[HttpPost("ControllerA")] public async Task<JsonResult> ControllerA([FromBody] ItemModel item) { ... } 

Error:

System.NullReferenceException: ‘Object reference not set to an instance of an object.’
<>8__1.IteModel was null.

This code has been working fine on version 2.1, but it stopped after migration.

Any idea?

Add Comment
1 Answer(s)

Solved by installing "Microsoft.AspNetCore.Mvc.NewtonsoftJSON" and adding the following to startup.cs

            services.AddControllers().AddNewtonsoftJson();             services.AddControllersWithViews().AddNewtonsoftJson();             services.AddRazorPages().AddNewtonsoftJson(); 
Answered on August 30, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.