当从.netcore 2.2迁移到3.0时,控制器 post [FromBody] Null问题
当从.netcore 2.2迁移到3.0时,控制器 post [FromBody] Null问题
我在2.2版本上有一个完全运行正常的程序,但当迁移到3.0版本并替换以下代码时出现问题:\n
public void ConfigureServices(IServiceCollection services) { ... services.AddMvc(); }
\n替换为services.AddControllers();
\n并将app.UseMvc();
替换为:\n
app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
\n其中一个控制器出现了问题(其他具有Post方法和[FromBody]的控制器运行正常)。\n出现问题的控制器和方法是:\n
[Route("api/vm")] public class MainController: Controller { [HttpPost] [Route("Process")] public IActionResult GetProcess([FromBody]ProcessModel[] process) { ... } }
\n模型如下:\n
public class ProcessModel { [JsonProperty("Name")] public string Name { get; set; } [JsonProperty("ExeName")] public string ExeName { get; set; } [JsonProperty("Path")] public string Path { get; set; } [JsonProperty("VersionPath")] public string VersionPath { get; set; } [JsonProperty("Id")] public string Id { get; set; } [JsonProperty("Status")] public string Status { get; set; } [JsonProperty("Ver")] public string Ver { get; set; } [JsonProperty("Args")] public string[] Args { get; set; } [JsonProperty("Instances")] public ListInstances { get; set; } [JsonProperty("Multiple")] public string Multiple { get; set; } }
\n我发出的对/api/vm/Process
的调用是:\n[\n {\n \"Name\": \"Test\",\n \"ExeName\": \"Test\",\n \"Multiple\": false,\n \"Path\": \"Test\",\n \"VersionPath\": \"Test\",\n \"Args\": {\n \"IsFile\": false\n }\n },\n {\n \"Name\": \"Test\",\n \"ExeName\": \"Test.exe\",\n \"Multiple\": false,\n \"Path\": \"Test\",\n \"VersionPath\": \"Test\",\n \"Args\": {\n \"IsFile\": false\n }\n }\n]\n
\n在生产环境中,应用程序运行得很好几个月。我所做的只是升级到.netcore 3,现在当我调试并进入控制器的方法时,process变量为null。\n注意:\n在应用程序首次出现问题时,我使用了此线程\nUsing \'UseMvc\' to configure MVC is not supported while using Endpoint Routing
问题出现在Multiple和Args这两个JSON属性上。
在JSON中,它们分别是布尔值和对象,但在Process模型中它们都是字符串。我不太确定它在Core 2.2中是如何工作的。
To fix this issue, we need to update the Process model to match the JSON properties. We can change the type of Multiple and Args to string in the Process model.
为了解决这个问题,我们需要更新Process模型以匹配JSON属性。我们可以将Multiple和Args的类型更改为字符串。
Here is the updated Process model:
下面是更新后的Process模型:
public class Process { public string Name { get; set; } public string Multiple { get; set; } public string Args { get; set; } }
After updating the Process model, the issue should be resolved and the [FromBody] attribute should work correctly.
更新Process模型后,问题应该得到解决,并且[FromBody]属性应该能够正常工作。
Note: It is important to make sure that the JSON properties match the model properties in terms of type and name. Otherwise, the [FromBody] attribute may not work as expected.
在迁移从.NET Core 2.2到3.0时,遇到了(Controller post [FromBody] Null issue when when migrating from .netcore 2.2 to 3.0)问题。问题的原因是ProcessModel
的Args
属性是string[]
类型,而在JSON中传递的是一个对象,导致模型绑定为null。
解决方法有两种:
第一种是将Args
属性的类型修改为string[]
,并在JSON中传递Args
的值作为字符串数组,如:"Args": ["IsFile:false"]
。
第二种是保持JSON不变,将Args
属性的类型修改为Dictionary<string, string>
,并添加[JsonProperty("Args")]
特性,如:public Dictionary<string, string> Args { get; set; }
。
在解决问题时,还需要添加对NewtonsoftJson
的引用,可以通过services.AddControllers().AddNewtonsoftJson()
来实现。
在迁移.NET Core 2.2到3.0时,遇到(Controller post [FromBody] Null issue when when migrating from .netcore 2.2 to 3.0)问题的原因是ProcessModel
的Args
属性类型与传递的JSON数据不匹配,导致模型绑定为null。解决方法是修改Args
属性的类型或修改JSON数据的格式,并添加对NewtonsoftJson
的引用。
在从 .netcore 2.2 迁移到 3.0 版本时,出现了一个(Controller post [FromBody] Null issue when when migrating from .netcore 2.2 to 3.0)问题。这个问题的原因是新的 System.Text.Json 不再支持自动类型转换,可能是出于性能考虑。解决方法是使用 "Newtonsoft serializer" 或使用自定义转换器。一些类的属性是字符串类型,但你却发送了布尔值(多个属性),整理的代码如下:
使用 Newtonsoft:
services.AddControllers().AddNewtonsoftJson();
使用 System.Text.Json 转换器示例:
注册你的转换器:
services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.Converters.Add(new MyInt32Converter()); });
更多关于从 Newtonsoft 迁移到 System.Text.Json 的信息可以参考:
learn.microsoft.com/en-us/dotnet/standard/serialization/…
这是微软的一个令人失望的地方,他们应该默认提供更多的兼容性,以便我们可以根据需要进行削减。