Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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#ASPNET WebApi对象的属性映射到Angular 2,_C#_Angular_Web Services_Asp.net Web Api_Naming Conventions - Fatal编程技术网

将C#ASPNET WebApi对象的属性映射到Angular 2,

将C#ASPNET WebApi对象的属性映射到Angular 2,,c#,angular,web-services,asp.net-web-api,naming-conventions,C#,Angular,Web Services,Asp.net Web Api,Naming Conventions,我对Angular 2 Herones教程进行了重新编程,使其在具有Web API的ASP.NET Web应用程序中运行,并与该Web API而不是内存中的Web API进行通信。 一切正常。只剩下一个问题: 在ASP.NET的服务环境和Angular环境中,我使用了object Hero。C#对象中属性的命名约定是以大写字母开头的propertyname。JS中的命名约定是以小写字母开始propertyname。我更喜欢在我的编码中遵循这个惯例。 如果我这样做的话,对象当然不会再在接收站点进行

我对Angular 2 Herones教程进行了重新编程,使其在具有Web API的ASP.NET Web应用程序中运行,并与该Web API而不是内存中的Web API进行通信。 一切正常。只剩下一个问题:

在ASP.NET的服务环境和Angular环境中,我使用了object Hero。C#对象中属性的命名约定是以大写字母开头的propertyname。JS中的命名约定是以小写字母开始propertyname。我更喜欢在我的编码中遵循这个惯例。 如果我这样做的话,对象当然不会再在接收站点进行适当的反序列化。 这通常是怎么处理的

ASP.NET控制器中的get(数组):

    // GET api/heroes 
public HttpResponseMessage Get()
{
  String heros =  JsonConvert.SerializeObject(GetHeroes().ToArray<Hero>());
  return new HttpResponseMessage()
  {
    Content = new StringContent(heros, System.Text.Encoding.UTF8, "application/json")
  };
}
hero.service.ts中的代码:

 getHeroes(): Promise<Hero[]> {
  return this.http.get(this.heroesUrl)
      .toPromise()
      .then(response => response.json() as Hero[])
      .catch(this.handleError);  }
Web服务为get提供的原始代码是:

[{"id":0,"name":"Zero"},{"id":11,"name":"Mr. Nice"},{"id":12,"name":"Narco"},{"id":13,"name":"Bombasto"},{"id":14,"name":"Celeritas"},{"id":15,"name":"Magneta"}]
问题是如何在C#中继续使用(这现在会导致问题)

如果我这样做,我不会得到任何错误。它只返回浏览器中的6个对象,只显示记录的内容,如下所示:

试试这个

public class Hero
{
  [JsonProperty(PropertyName = "id")]
  public int Id { get; set; }
  [JsonProperty(PropertyName = "name")]
  public string Name { get; set; }
}

文档

将以下内容添加到Global.asax.cs中的应用程序启动中。这将强制Json序列化为驼峰式

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
        {
            Formatting = Formatting.Indented,
            TypeNameHandling = TypeNameHandling.Objects,
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };

根据我的经验,从c#到Angular的属性映射不区分大小写。只要属性的拼写正确,就不会引起任何问题。您是否有任何错误?不区分大小写?有趣。回答你的问题:我没有得到任何错误。它只返回浏览器中的6个对象,而不显示记录的内容。这意味着对象被反序列化,只有属性不匹配。不能,因为我没有足够的“信誉点”arghhh..想要询问的所有问题都已被询问。。。甚至不能结束这个问题。
    public class Hero
{
  public int Id { get; set; }
  public string Name { get; set; }
}
public class Hero
{
  [JsonProperty(PropertyName = "id")]
  public int Id { get; set; }
  [JsonProperty(PropertyName = "name")]
  public string Name { get; set; }
}
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
        {
            Formatting = Formatting.Indented,
            TypeNameHandling = TypeNameHandling.Objects,
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };