4Manuals

  • PDF Cloud HOME

从.netcore 2.2迁移到3.0时,控制器发布[FromBody]空问题 Download

    获取Hough线的交点OpenCV C ++ 如何将字符串“ text1”设置为包含项目text1,text2,test3的组合框的选定项目? 为什么System.Net.Mail.MailAddress构造函数在域部分中解析带有斜杠“ /”的电子邮件? Android 10,API29:在应用程序文件夹中使用C ++库编写文件会使应用程序崩溃 Unity-LineRenderer没有出现在版本(2D)上 按下按钮后,电话中未显示通知 ASP.net,使用WebConfig httpErrors重定向到区域内的控制器不起作用 获取会议室详细信息 我们如何将参数Line转换为Station? 网络核心:查找实体框架核心的主键和反射

我在2.2版中拥有一个功能齐全的程序 迁移到3.0版并替换

时
public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddMvc();
}

使用services.AddControllers();

并替换app.UseMvc();

使用:

app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

其中一个控制器损坏。 (其他具有Post Method和[FromBody]的控制器也可以正常工作) 控制器和坏掉的方法是:

[Route("api/vm")]
public class MainController: Controller
{
    [HttpPost]
    [Route("Process")]
    public IActionResult GetProcess([FromBody]ProcessModel[] process)
    {
         ...
    }
}

模型:

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 List<ProcessDetails> Instances { get; set; }
    [JsonProperty("Multiple")]
    public string Multiple { get; set; }
}  

我打给/api/vm/Process的电话:

[
    {
        "Name": "Test",
        "ExeName": "Test",
        "Multiple": false,
        "Path": "Test",
        "VersionPath": "Test",
        "Args": {
            "IsFile": false
        }
    },
    {
        "Name": "Test",
        "ExeName": "Test.exe",
        "Multiple": false,
        "Path": "Test",
        "VersionPath": "Test",
        "Args": {
            "IsFile": false
        }
    }
]

该应用在生产中正常工作了几个月。我所做的就是升级到.netcore 3,现在,当我调试并进入控制器的方法时,过程变量为空

注意: 当应用程序最初损坏时,我使用了此线程 Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing

3 个答案:

答案 0 :(得分:1)

自动类型转换在新的System.Text.Json中不可用,可能是出于性能原因。您应该使用“ Newtonsoft serializer”或使用自定义转换器。您的某些类属性是字符串,但您正在发送int的bool(Multiple属性)。

使用Newtonsoft: https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#jsonnet-support

System.Text.Json转换器示例:

https://github.com/dotnet/corefx/blob/master/src/System.Text.Json/tests/Serialization/CustomConverterTests.Int32.cs

像这样注册您的转换器:

services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.Converters.Add(new MyInt32Converter());
});

答案 1 :(得分:1)

原因是您的ProcessModel的{​​{1}}类型为string[]:

Args

在json中,您将其作为对象传递,导致模型绑定为空

public string[] Args { get; set; }

像"Args": { "IsFile": false } 一样通过Args

string[]

或者,如果您确实想更改json,请将"Args": ["IsFile:false"] 修改为Dictionary类型:

Args

请记住要像其他人所说的那样添加对[JsonProperty("Args")] public Dictionary<string, string> Args { get; set; } 的引用

NewtonsoftJson

答案 2 :(得分:0)

Multiple和Args JSON属性中的问题。
在JSON中,它们既是布尔对象又在对象中,但是在Process模型中,它们都是字符串。我不太确定它在Core 2.2中如何工作。



Similar searches
    需要帮助修复在Kali Linux上mysql安装中的错误 如何将PHP变量传递到数据层中的多个GA产品数组 无法绑定百里香从jQuery选择多个模型对象 一个使用本地JS模块在浏览器和NodeJS上运行的.mjs文件 Samsung RS2544 Refrigerator User Manual