esa*_*lva 5 ajax asp.net-mvc .net-core fetch-api asp.net-core
我刚刚迁移到 .NET Core 3.1,在我必须更新的不同内容中,我无法让我的 ajax 调用工作。
当我进行 ajax 调用时,没有任何数据绑定到我的操作中的对象参数。
这就是我在剃须刀页面中拨打电话的方式
fetch('/Administration/UpdateUserStatus', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'RequestVerificationToken': $('input[name="__RequestVerificationToken"]').val()
},
body: JSON.stringify(data)
})
Run Code Online (Sandbox Code Playgroud)
我的控制器动作
[HttpPost]
public IActionResult UpdateUserStatus([FromBody]User user) {
...
}
Run Code Online (Sandbox Code Playgroud)
在我上面的示例代码中,调试我可以看到user作为传入的代码null,因此来自 ajax 调用的数据不会绑定到User对象。
如果我删除[FromBody],user不会再进来null,但数据仍然没有绑定。所有属性为 null 或默认值。
在 .NET Core 2.1 中一切正常。
这是我需要添加的东西Startup.cs吗?也许我缺少一个新配置?
谢谢
I solved my issue. Json.NET was removed from .NET Core 3.0 so I had to add a reference to package Microsoft.AspNetCore.Mvc.NewtonsoftJson (NuGet) then under the ConfigureServices method add .AddNewtonsoftJson() to Razor Pages, like so
services.AddRazorPages().AddNewtonsoftJson();
Run Code Online (Sandbox Code Playgroud)
After that, everything is working as before.
| 归档时间: |
|
| 查看次数: |
1348 次 |
| 最近记录: |