Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NETCore3.0API没有';不能用连字符绑定属性_C#_.net_Json_Api_.net Core - Fatal编程技术网

C# .NETCore3.0API没有';不能用连字符绑定属性

C# .NETCore3.0API没有';不能用连字符绑定属性,c#,.net,json,api,.net-core,C#,.net,Json,Api,.net Core,由于上次没有得到很好的回答,因此对该问题进行了审核。希望我已经提供了下面所需的所有信息。 我有一个基本的API控制器,我的Json对象似乎没有正确绑定到模型。根对象绑定,但名称中带有连字符的属性不绑定。很遗憾,我无法在属性名称中删除连字符 如何使属性正确绑定 using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; namespace TestCoreAPI.Controllers { [Route("api/[controller]

由于上次没有得到很好的回答,因此对该问题进行了审核。希望我已经提供了下面所需的所有信息。

我有一个基本的API控制器,我的Json对象似乎没有正确绑定到模型。根对象绑定,但名称中带有连字符的属性不绑定。很遗憾,我无法在属性名称中删除连字符

如何使属性正确绑定

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace TestCoreAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        // POST: api/Test
        [HttpPost]
        public string Post([FromBody] TestPayload testPayload)
        {
            if (testPayload == null)
            {
                return "Test payload is empty";
            }

            if (string.IsNullOrWhiteSpace(testPayload.TestProperty))
            {
                return "Test property is empty";
            }

            return "Valid input - " + testPayload.TestProperty;
        }
    }

    [JsonObject("test-payload")]
    public class TestPayload
    {
        [JsonProperty(PropertyName = "test-property")]
        public string TestProperty { get; set; }
    }
}
这是我对API的调用

POST /api/test HTTP/1.1
Content-Type: application/json

{"test-property":"some string value"}

您是否正在将
AddNewtonsoftJson()
添加到
Startup.ConfigureServices
中的服务中?如果没有,则使用的是新的
System.Text.Json
,而不是Newtonsoft。我认为您需要执行
AddNewtonSoftJson()
,因为我非常确定
System.Text.Json
不支持“kebab case”绑定


您是否在
Startup.ConfigureServices
中将
AddNewtonsoftJson()
添加到您的服务中?如果没有,则使用的是新的
System.Text.Json
,而不是Newtonsoft。我认为您需要执行
AddNewtonSoftJson()
,因为我非常确定
System.Text.Json
不支持“kebab case”绑定


Net Core 3.1不绑定连字符。无论哪种方式,这两个选项都是
Newtonsoft.Json
或new-in-core-3
System.Text.Json
,它们使用稍微不同的
属性
名称:

public class PostModel
{
    //This one if you are using Newtonsoft.Json
    //The Nuget dependency is Microsoft.AspNetCore.Mvc.NewtonsoftJson
    [JsonProperty(PropertyName = "kebab-case-json-field")]

    //This one of you are using the new System.Text.Json.Serialization
    [JsonPropertyName("kebab-case-json-field")]

    public string kebabCaseProperty { get; set; }
}
同时,在
Startup.cs
中,要使用Newtonsoft,需要
AddMvc()
,而对于新的System.Text.Json,则不需要。这两种方法对我都有效:

public void ConfigureServices(IServiceCollection services)
{
    //if using NewtonSoft
    services.AddMvc().AddNewtonsoftJson();

    //if using System.Text.Json.
    //This is the code that core 3 `dotnet new webapi` generates
    services.AddControllers();
}

NETCore3.1不绑定连字符。无论哪种方式,这两个选项都是
Newtonsoft.Json
或new-in-core-3
System.Text.Json
,它们使用稍微不同的
属性
名称:

public class PostModel
{
    //This one if you are using Newtonsoft.Json
    //The Nuget dependency is Microsoft.AspNetCore.Mvc.NewtonsoftJson
    [JsonProperty(PropertyName = "kebab-case-json-field")]

    //This one of you are using the new System.Text.Json.Serialization
    [JsonPropertyName("kebab-case-json-field")]

    public string kebabCaseProperty { get; set; }
}
同时,在
Startup.cs
中,要使用Newtonsoft,需要
AddMvc()
,而对于新的System.Text.Json,则不需要。这两种方法对我都有效:

public void ConfigureServices(IServiceCollection services)
{
    //if using NewtonSoft
    services.AddMvc().AddNewtonsoftJson();

    //if using System.Text.Json.
    //This is the code that core 3 `dotnet new webapi` generates
    services.AddControllers();
}

通常的原因是JSON与模型不匹配。如果没有看到您的模型,就无法确定这是否是您的问题。在有人询问之前,这里也是模型定义
[JsonObject(“test payload”)]公共类TestPayload{[JsonProperty(“test string”)]公共字符串TestString{get;set;}
,您的模型不匹配。您的根对象,具有作为子对象的属性“测试有效负载”。该子对象有一个属性
“测试字符串”
,它是一个
字符串
属性。这里需要表示这两个对象,而不仅仅是嵌套对象。JSON在C#中表示起来非常简单。每当你看到
{}
你就有了一个对象(用C#中的
表示),每当你看到
[]
你就有了一个数组(用C#中的
列表
T[]
等表示),其他一切都是一个简单的属性,比如
字符串
int
。如果您不确定,Visual Studio有
Edit>Paste Special>Paste JSON as class
。通常的原因是JSON与模型不匹配。如果没有看到您的模型,就无法确定这是否是您的问题。在有人询问之前,这里也是模型定义
[JsonObject(“test payload”)]公共类TestPayload{[JsonProperty(“test string”)]公共字符串TestString{get;set;}
,您的模型不匹配。您的根对象,具有作为子对象的属性“测试有效负载”。该子对象有一个属性
“测试字符串”
,它是一个
字符串
属性。这里需要表示这两个对象,而不仅仅是嵌套对象。JSON在C#中表示起来非常简单。每当你看到
{}
你就有了一个对象(用C#中的
表示),每当你看到
[]
你就有了一个数组(用C#中的
列表
T[]
等表示),其他一切都是一个简单的属性,比如
字符串
int
。如果您不确定,Visual Studio有
Edit>Paste Special>Paste JSON as class
。对于newtonsoft,您需要服务
.AddMvc().AddNewtonsoftJson()
,而新的框架JSON不需要mvc,它只需要
AddControllers()
对于newtonsoft,您需要服务
.AddMvc().AddNewtonsoftJson().AddNewtonsoftJson()
尽管新的框架Json不需要mvc,但它只需要
AddControllers()