asp.net-mvc - 从 Web api + AngularJS 中的 JSON 中删除 ↵

标签 asp.net-mvc angularjs json.net asp.net-web-api asp.net-web-api2

我正在使用 Web API 2 + AngularJS 将数据返回到 Angular Controller ,但我不知道为什么我在 json 结果中不断收到此字符 ↵。

我的 ASP Controller 有这个方法。

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);
    }

我在我的 Angular Controller 中得到了这个(我尝试使用 $http、$resource 和 Restangular 并得到了相同的结果):

http://i.imgur.com/LBAn3y3.jpg

我在 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.....)

没有别的。

你能帮我吗?

最佳答案

我不知道你的 AngularJS Controller 后面是什么,但我会用这种方式删除这个 ↵ 输入符号。

使用正则表达式在 AngularJS Controller 中创建一些函数来检查字符串是否包含 ↵,例如:

  $scope.hasEnter = function(str) {
        var enter= /\n/gi;
        if (enter.test(str)) {
            return true;
        } else
            return false;
    };

如果这是真的,只需将其替换为您需要的字符即可。

希望这个想法对你有帮助。

关于asp.net-mvc - 从 Web api + AngularJS 中的 JSON 中删除 ↵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27478204/

相关文章:

c# - NuGet 包将非托管 dll 复制到根目录而不是 bin

javascript - JQuery 自动完成后获取 ID 的值

javascript - Angular httpBackend 无法与 waitsFor 一起使用并运行(期望 promise )

javascript - 在 AngularJS 中深入 $watch 一个集合

c# - 将带有数组的 json 结构展平为多个没有数组的平面对象

.net - 如何使用 "dotnet"工具构建 .sqlproj 项目?

asp.net-mvc - 无法从 VS 2015 中的 Controller 添加 View : "There was an error running the selected code generator"

javascript - AngularJS 从 HTML 中的 <p> 获取值

c# - 如何向 Json.NET 输出添加注释?

c# - .Net Core 3.0.100-preview6 - API Json 响应总是驼峰式,但我的类(class)不是