C# 如何摆脱MVCWebAPI方法返回的$type属性?

C# 如何摆脱MVCWebAPI方法返回的$type属性?,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我正在调用我使用MVC4 ApicController编写的web API 它返回一个字典,如下所示: return Request.CreateResponse<Dictionary<int, int>>(HttpStatusCode.OK, histogram); 此对象有一个$type属性,该属性引用它对应的.NET类型。我不想把这个发给我的客户 我如何摆脱$type属性?我发现了发生的事情。基本上,我是对自己做的 我需要更改我的WebApiConfig.cs文件

我正在调用我使用MVC4 ApicController编写的web API

它返回一个
字典
,如下所示:

return Request.CreateResponse<Dictionary<int, int>>(HttpStatusCode.OK, histogram);
此对象有一个
$type
属性,该属性引用它对应的.NET类型。我不想把这个发给我的客户


我如何摆脱
$type
属性?

我发现了发生的事情。基本上,我是对自己做的

我需要更改我的
WebApiConfig.cs
文件:

public static void Register(HttpConfiguration config)
{
    // ...

    // remove this line:
    config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects;
}
删除该行后,
$type
属性不再显示在API控制器方法的结果中


这就引出了一个问题:在传入web API调用期间,是否可以使用
$type
属性实例化正确的类,同时将
$type
属性保留在传出的JSON或XML之外?

在更好地转换 后端中jsson的柱状图

string json = JsonConvert.SerializeObject( histogram, Formatting.Indented, new   JsonSerializerSettings
       {
        TypeNameHandling = TypeNameHandling.All,
        TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
      });

return Request.CreateResponse(HttpStatusCode.OK, json);

不要返回字典,它只针对.NET,对于不是.NET客户端的api的任何使用者来说都没有意义。我的api调用应该只是作为JSON对象返回一些键/值对。我知道字典是针对.NET的;这恰好是键/值对在我的控制器中的表示方式。
$type
属性不在我返回的字典中。MVC似乎正在自行添加属性。请尝试返回
IEnumerable
string json = JsonConvert.SerializeObject( histogram, Formatting.Indented, new   JsonSerializerSettings
       {
        TypeNameHandling = TypeNameHandling.All,
        TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
      });

return Request.CreateResponse(HttpStatusCode.OK, json);