【发布时间】:2015-02-13 04:54:08
【问题描述】:
我正在使用 web API 2 + AngularJS 将我的数据返回到角度控制器,但我不知道为什么我的 json 结果中不断出现这个字符 ↵。
我的 ASP 控制器有这个方法。
public IHttpActionResult GetAditionalFieldCOnfiguration()
{
var ListAdditionalFields = new List<rgAdditionalField>()
{
new rgAdditionalField(){ Type="text", Label="Nombre", Model="first", Val="Carlo" },
new rgAdditionalField(){ Type="text", Label="Apellido", Model="last", Val="Carlos prueba"},
new rgAdditionalField(){ Type="text", Label="Direc", Model="direccion", Val="los alpes"},
};
return Ok(ListAdditionalFields);
}
我在我的角度控制器中得到了这个(我尝试了 $http、$resource 和 Restangular 并得到了相同的结果):
我在 WebApiConfig 中尝试了不同的配置,但没有任何效果,配置:
public static JsonSerializerSettings GetSettingListFormat(rgJsonSettingType type)
{
switch (type)
{
case rgJsonSettingType.List:
return new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.None, ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
case rgJsonSettingType.Single:
return new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
case rgJsonSettingType.SingleCamelCase:
return new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, ContractResolver = new CamelCasePropertyNamesContractResolver() };
case rgJsonSettingType.ListCamelCase:
return new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.None, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, ContractResolver = new CamelCasePropertyNamesContractResolver() };
}
return new JsonSerializerSettings();
}
我尝试使用 HttpResponseMessage 并返回:
return Request.CreateResponse(HttpStatusCode.OK, ListAdditionalFields, JsonConfiguration.....)
没有别的了。
你能帮帮我吗?
【问题讨论】:
-
不只是您的调试工具或任何表明您有回车的东西吗?
-
但是为什么我的代码可以在 mvc 控制器中工作,而 web api 控制器却不能,我使用相同的 json 配置得到了不同的结果。谢谢。
标签: asp.net-mvc angularjs json.net asp.net-web-api asp.net-web-api2