Typescript为什么我的Get请求以驼峰形式出现?

我正在从我的GET应用向Angular 8 and typescript应用发出ASP.NET CORE 3 web API请求。返回的对象是骆驼案,我想留在帕斯卡案。

在TypeScript中看起来像这样

    this.httpClient.get<Task>("Task/GetTask/" + task.id).subscribe((ganttTask: Task) => {

        console.log(ganttTask);
    });

这是TS中的任务对象

export class Task{
    public TaskID: string;
    public SubprojectID: string;
    public Description: string;
    public Color: string;
    public SortOrder: number;
    public WbsColor: string;
    public TextColor: string;
    public StartDate: Date;
    public DurationHours: Number;
    public EndDate: Date;
    public PlannedStartDate: Date;
    public PlannedEndDate: Date;

    constructor(){
        //default colors
        this.Color = '#ABD8E6'; 
        this.TextColor = '#000000';
        this.WbsColor = '#000000';
    }
}

这是C#中的任务对象

public class Task
    {
        public Task() {

        }

        [Key]
        public Guid TaskID {get;set;}
        public Guid SubprojectID { get; set; }
        public DateTimeOffset StartDate { get; set; }
        public int DurationHours { get; set; }
        public DateTimeOffset EndDate { get; set; }
        public DateTimeOffset? PlannedStartDate { get; set; }
        public DateTimeOffset? PlannedEndDate { get; set; }
        public string Description { get; set; }
        public string Color { get; set; }
        public string TextColor { get; set; }
        public string WbsColor { get; set; }
        public int SortOrder { get; set; }
    }

这是返回数据的示例

{

    color: "#ed0732"
    description: "test 4"
    durationHours: 4
    endDate: "2019-11-04T22:00:00+00:00"
    plannedEndDate: null
    plannedStartDate: null
    sortOrder: 0
    startDate: "2019-11-04T12:00:00+00:00"
    subprojectID: "c9335b34-8b2d-42de-8cad-ba0ca1a79a5d"
    taskID: "e9cef6f6-82a8-4f9c-9fda-692f2a84cd24"
    textColor: "#f7dcf7"
    wbsColor: "#000000" 
  }

这仅发生在该实体上,我猜是因为日期以字符串形式返回。这个Task对象将增长为具有约100个属性。我宁愿不必使用这么多属性来转换此对象或任何其他对象,除非我必须这样做。有没有办法将返回的类型映射到Task对象?我应该改变日期的处理方式吗?

lijunjiji 回答:Typescript为什么我的Get请求以驼峰形式出现?

您的jsonSerializer会转换为驼色的情况,您需要在C#中配置jsonSerialization。

var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
//Set to use which ever resolver you need e.g pascal resolver;
settings.ContractResolver = new DefaultContractResolver(); 
,

您可以考虑将jsonSerializer配置为在通话时使用驼峰式大小写。

本文链接:https://www.f2er.com/3163475.html

大家都在问