简体   繁体   English

迁移到 ASP.Net Core MVC 时 JSON 序列化/反序列化不起作用

[英]JSON serialization/deserialization not working when migrating to ASP.Net Core MVC

We are migrating a site from ASP MVC .Net Framework to .Net Core.我们正在将一个站点从 ASP MVC .Net Framework 迁移到 .Net Core。 In many cases we do ajax POST requests from the page to the Controller:在很多情况下,我们从页面到控制器做 ajax POST 请求:

js: js:

var request = { Name: "bar" };
$.ajax({
    url: "/Home/foo",
    type: "POST",
    data: JSON.stringify(request),
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    success: function (response) {
        console.log();
    }
});

c# Controller: C# 控制器:

[HttpPost]
public async Task<JsonResult> foo(FooRequest request)
{
    //request.Name is empty
    return Json(new { msg = "Success" });
}

c# Model: c#模型:

public class FooRequest
{
    public string Name { get; set; }
}

This all works in .Net Framework, but fails in Core were all properties of the received object are null .这一切都适用于 .Net Framework,但在 Core失败,因为接收到的对象的所有属性都是 null

I've tried using both AddJsonOptions :我试过同时使用AddJsonOptions

services.AddControllersWithViews()
    .AddJsonOptions(option =>
    {
        option.JsonSerializerOptions.PropertyNamingPolicy = null;
        option.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
    });

...and AddNewtonsoftJson : ...和AddNewtonsoftJson

services.AddControllersWithViews()
    .AddNewtonsoftJson(option =>
    {
        option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
    });

...but none of them works. ...但它们都不起作用。

What do I need to do to get the same behavior in .Net Core as I do in .Net Framework?我需要做什么才能在 .Net Core 中获得与在 .Net Framework 中相同的行为?

Update更新

I get the same behavior calling the endoint from Postman (request.Name is null):我从 Postman 调用 endint 得到相同的行为(request.Name 为空):

POST /home/foo HTTP/1.1
Host: localhost:44393
Content-Type: application/json

{ "Name": "bar" }

I've also tried to serializing the request:我还尝试序列化请求:

var request = { Name: "bar" };
$.ajax({
    url: "/Home/foo",
    type: "POST",
    data: request,
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    success: function (response) {
        console.log();
    }
});

...but with the same result ...但结果相同

Try adding the FromBodyAttribute to your parameter:尝试将FromBodyAttribute添加到您的参数中:

[HttpPost]
public async Task<JsonResult> foo([FromBody] FooRequest request)
{
    //request.Name is empty
    return Json(new { msg = "Success" });
}

You can omit this attribute, if you decorate your controller with the ApiControllerAttribute你可以省略这个属性,如果你用ApiControllerAttribute装饰你的控制器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net Core 中的 JSON 序列化/反序列化 - JSON serialization/deserialization in ASP.Net Core 从 MVC 迁移到 ASP.NET Core 3.1 中的端点路由时,具有角色的 AuthorizeAttribute 不起作用 - AuthorizeAttribute with Roles not working when migrating from MVC to Endpoint Routing in ASP.NET Core 3.1 JSON 自定义序列化和反序列化 C# asp.net 内核中的现有类 - JSON custom serialization and deserialization of existing classes in C# asp.net core 更改单个 ASP.NET 内核 controller 的 JSON 反序列化/序列化策略 - Change the JSON deserialization/serialization policy for single ASP.NET Core controller 忽略ASP.NET Core 2中XML序列化/反序列化的属性 - Ignore property from XML serialization/deserialization in ASP.NET Core 2 ASP.NET Core Web API自动JSON参数反序列化不起作用 - ASP.NET Core Web API Automatic JSON Parameter Deserialization Not Working ASP.NET 内核处理 JSON 反序列化问题 - ASP.NET Core handling JSON deserialization problems ASP.NET Core 中每个控制器的不同 JSON 反序列化设置 - Different JSON deserialization settings per controller in ASP.NET Core Json反序列化数据库字段和asp.net MVC - Json deserialization of database field and asp.net mvc ASP.NET JSON反序列化 - Asp.net json deserialization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM