【问题标题】:.Net properties are missing in the response got from AngularJs Ajax call to Asp.Net Web Api从 AngularJs Ajax 调用 Asp.Net Web Api 得到的响应中缺少 .Net 属性
【发布时间】:2015-10-15 11:02:32
【问题描述】:

我正在使用 AngularJS $http 对 Asp.Net Web Api 进行 ajax 调用。

                $http({
                    method: 'GET',
                    url: '/api/PatientCategoryApi/PatCat',
                    params: dataTemp,
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                }).then(function successCallback(response) {
                    $scope.modelObject.resultData = response.data;
                    alert(JSON.stringify(response.data));
                }, function errorCallback(response) {
                    $scope.modelObject.result = response;
                });

web api 操作具有以下签名

public PaginatedList<PatientCategory> PatCat(PaginatedRequestCommand cmd){...}

PaginatedList 类型如下。

public class PaginatedList<T> : List<T>
{
    public int PageIndex { get; private set; }
    public int PageSize { get; private set; }
    public int TotalCount { get; private set; }
    public int TotalPageCount { get; private set; }

    public bool HasPreviousPage
    {
        get
        {
            return (PageIndex > 1);
        }
    }

    public bool HasNextPage
    {
        get
        {
            return (PageIndex < TotalPageCount);
        }
    }
}

我已经删除了 ctor,因为这里不需要它。所以它只是一个 T 列表的包装器,在这种情况下,T 是 PatientCategory。

我可以在 ajax 调用之后从响应对象访问列表。但我无法找到 PaginatedList 的属性,例如 响应对象中的 PageIndex、PageSize 等。即使我使用提琴手,我也可以清楚地看到 PatientCategory 对象的数组,但是因为缺少 PaginatedList 的属性。

[{"Id":1,"Code":"H05120000003","Name":"Regular","Description":"The Regular Patient Category","ModifiedTime":"2015-10-01T19:34:33.727","History":null,"MyXmlColumn":null},{"Id":2,"Code":"BH130000001","Name":"STAFF_CHGD_TO_REGULAR","Description":null,"ModifiedTime":"2015-10-01T19:46:07.093","History":null,"MyXmlColumn":null},{"Id":3,"Code":"BH130000001","Name":"STAFF_CHGD_TO_REGULAR","Description":null,"ModifiedTime":"2015-10-01T19:52:03.773","History":null,"MyXmlColumn":null}]

那么我怎样才能在响应中访问这些属性呢?

Fiddler 中显示的请求和响应如下。

请求

GET http://localhost:50849/api/PatientCategoryApi/PatCat?Page=1&Take=3 HTTP/1.1
Host: localhost:50849
Connection: keep-alive
Accept: application/json, text/plain, */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Referer: http://localhost:50849/PatientCategory/Index
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

回应

HTTP/1.1 200 OK
Content-Length: 479
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
X-SourceFiles: =?UTF-8?B?RDpcQlZIXEF2YmhIaXNcQXZiaEhpcy5XZWJcYXBpXFBhdGllbnRDYXRlZ29yeUFwaVxQYXRDYXQ=?=
X-Powered-By: ASP.NET
Date: Thu, 15 Oct 2015 01:43:57 GMT

[{"Id":1,"Code":"H05120000003","Name":"Regular","Description":"The Regular Patient Category","ModifiedTime":"2015-10-01T19:34:33.727","History":null,"MyXmlColumn":null},{"Id":2,"Code":"BH130000001","Name":"STAFF_CHGD_TO_REGULAR","Description":null,"ModifiedTime":"2015-10-01T19:46:07.093","History":null,"MyXmlColumn":null},{"Id":3,"Code":"BH130000001","Name":"STAFF_CHGD_TO_REGULAR","Description":null,"ModifiedTime":"2015-10-01T19:52:03.773","History":null,"MyXmlColumn":null}]

【问题讨论】:

    标签: ajax angularjs asp.net-web-api


    【解决方案1】:

    Asp.net web api 使用 NewtonJson.dll 进行 Javascript 序列化。当newtonjson找到一个实现IEnumerable的类时,它会将这个对象序列化成这样的Javascript对象[{},{}],看来newtonjson只序列化了IEnumerable对象,其他属性都被忽略了。您也可以将 NewtonSoft.Json.JsonObject 添加到 PagenatedList 类中,此属性将告诉 newtonjson 将数据序列化为像 {p1: "", p2: ""} 这样的 javascript 普通对象,但您会发现 IEnumebrable 数据丢失.为了解决这个问题,您应该自定义一个继承自 JsonConverter 的新 CustomJsonConverter,并将该属性添加到您的 PagenatedList 中,如下所示:

    [Newtonsoft.Json.JsonConverter(typeof(CustomJsonConverter))
    public class PagenatedList<T>:List<T>
    //your class code
    

    【讨论】:

    • 哦..不,这不是一个好消息。无论如何,当我搜索时,似乎没有其他选择。我认为 $http ajax 调用中的一些简单调整就可以完成这项工作。
    • 或者您可以尝试修改您的自定义类“PaginatedList”,将其从继承自列表更改为包含“列表”属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2021-08-08
    • 2015-05-07
    • 2016-12-18
    • 1970-01-01
    • 2018-08-03
    相关资源
    最近更新 更多