【问题标题】:DataMember Attribute is not honored in dotnet core 3.0dotnet core 3.0 中不支持 DataMember 属性
【发布时间】:2019-09-16 23:03:03
【问题描述】:

我创建了一个示例 dotnet core 3.0 Web API 项目并进行了以下更改,

  1. 创建一个模型类TestData
using System.Runtime.Serialization;
namespace WebApplication17.Models
{
    [DataContract]
    public class TestData
    {
        [DataMember(Name = "testaction")]
        public string Action { get; set; }
    }
}

然后我在控制器 WeatherForecastController 中进行了更改,以添加一个 post 端点

[HttpPost("package/{packageName}/version/{version}")]
public void Post(string packageName, string version, [FromBody] TestData activityPayload)
{
    Console.WriteLine(activityPayload.Action);
}

现在我打了邮递员的电话或用身体卷曲

{   
    "testaction": "action"  
}

仍然在WeatherForecastControllerPost方法中,activityPayload.Actionnull

我期待它是“行动”

【问题讨论】:

    标签: c# .net-core asp.net-core-webapi .net-core-3.0 asp.net-core-3.0


    【解决方案1】:

    默认情况下,Asp.Net Core 3 不支持 [DataContract][DataMember],而且根据这个 Github 问题,他们似乎不会很快添加它

    System.Text.Json support to System.Runtime.Serialization

    如果您想切换回使用 Newtonsoft.Json 的先前默认设置,它确实尊重这些属性,那么您必须执行以下操作:

    1. 安装Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet 包。

    2. ConfigureServices() 中添加对AddNewtonsoftJson() 的调用

    public void ConfigureServices(IServiceCollection services) {
        //...
    
        services.AddControllers()
            .AddNewtonsoftJson(); //<--
    
        //...
    }
    

    【讨论】:

      【解决方案2】:

      从 .NET Core 3.0 RC1 开始,System.Text.Json 库不支持 System.Runtime.Serialization 属性。您可以在 GitHub 上找到一个问题,该问题正在跟踪此遗漏,但目前看来没有任何改变它的意图。

      选项 1:Newtonsoft.Json

      在此期间您可以做的是切换到使用 Newtonsoft.Json 作为 ASP.NET Core 3.0 的 JSON 序列化程序,这应该恢复此功能(以不利用更快的 System.Text.Json 解析器为代价)。

      首先,在你的项目中添加对Microsoft.AspNetCore.Mvc.NewtonsoftJson包的引用:

      <Project Sdk="Microsoft.NET.Sdk.Web">
        <ItemGroup>
          <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" />
        </ItemGroup>
      </Project>
      

      然后调用你的服务集合上的扩展。

      public void ConfigureServices(IServiceCollection services)
      {
          services.AddControllers()
                  .AddNewtonsoftJson();
      }
      

      选项 2:使用 System.Text.Json.Serialization

      另一方面,如果您愿意在没有 System.Runtime.Serialization 属性的情况下定义模型并改用 System.Text.Json.Serialization 属性,那么您可以执行以下操作:

      using System.Text.Json.Serialization;
      namespace WebApplication17.Models
      {
          public class TestData
          {
              [JsonPropertyName("testaction")]
              public string Action { get; set; }
          }
      }
      

      您可以在此处找到支持的属性的完整列表:https://docs.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonpropertynameattribute?view=netcore-3.0

      【讨论】:

        【解决方案3】:

        [JsonPropertyName("testaction")] 属性添加到Action 属性。这应该可以解决您的问题。

        更多信息请看这里: https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-01-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多