简体   繁体   中英

Why, if WebAPI action method returns a C# list, in Javascript / Backbone an object is received instead of an array?

Situation 1

I have C# WebAPI with an action that returns a List

    [HttpGet]
    [Route("/views/documents")]
    [ValidateModelState]
    [SwaggerOperation("ViewsDocumentsGet")]
    [SwaggerResponse(statusCode: 200, type: typeof(List<Documents>), description: "...")]
    public virtual IActionResult ViewsDocumentsGet()
    {
        var result = new List<Documents>();
        ...
        return new ObjectResult(result);
    }

On the client side, there is a Backbone model, that fetches the documents

import { Model } from "backbone";
export default Model.extend({
initialize: function() {
    this._url = arguments[0];
},

url: function(strSwitch) {
    return this._url + `/views/documents`;
},

...
});

I call fetch on the model

  const response = await this.DocumentsModel.fetch();
  console.log(typeof response, response);

The console.log returns

object (14) [
0: {DocType: "...", ...}
1: {DocType: "...", ...}
2: ... 
length: 14
__proto__: Array(0)

response has type object. I am expecting array.


Situation 2

If I change the WebAPI action method to return an object that wraps a list, then response is an array.

    [DataContract]
    public class DocumentWrapper
    {
        ...
        [DataMember]
        public List<Document> Documents { get; set; }
        ...
    }

    [HttpGet]
    [Route("/views/documents")]
    [ValidateModelState]
    [SwaggerOperation("ViewsDocumentsGet")]
    [SwaggerResponse(statusCode: 200, type: typeof(DocumentWrapper), description: "...")]
    public virtual IActionResult ViewsDocumentsGet()
    {
        var result = new DocumentWrapper();
        ...
        return new ObjectResult(result);
    }

Now the response is still of type object, but the object has a property Documents which is an array.

object {Documents: Array(14)}
        Documents: Array(14)
        0: {DocType: "...", ...}
        1: {DocType: "...", ...}
        2: ... 
        length: 14
        __proto__: Array(0)
    __proto__: Object

In situation 1, I know how to convert the object to an array. But I do not want to do that. I want backbone to fetch an array from the API without me having to do any conversion.

Questions

  1. Why is the C# list not automatically an array?
  2. How to tell backbone to fetch an array instead of an object?

Backbone model fetch combines the state of the model with attributes fetched from the server by calling Backbone.sync . By default, it uses jQuery.ajax to make a RESTful JSON request and returns a jqXHR . You can override it in order to use a different persistence strategy but it will always return an jqXHR object because internally it is making ajax requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM