例如,当您使用 ASP.NET MVC 创建单页应用程序和使用 REST 的一些 Web API 控制器时,往往会向服务器发出大量请求,因为视图通常依赖于一些复杂的数据。在一个普通的网络项目中,我肯定会尝试创建尽可能少的请求,也许这适用于单页应用程序?就我而言,我的应用程序依赖于两种类型的对象和一些相同类型的集合。在客户端,我正在使用 Backbone.js,我的控制器正在处理每个 for 的一种类型Get()
,Get(int id)
比如应该实现 REST。就我而言,这不是一个开放的 API,只是我的应用程序在使用它。这是我的应用程序所依赖的 JSON 示例。
{
CurrentPage: {
Heading: "Home",
Id: "pages/193",
Metadata: {
Name: "Home",
Title: null,
Keywords: null,
Description: null,
DisplayInMenu: true,
Published: "/Date(1334499539404)/",
Changed: "/Date(1334499539404)/",
ChangedBy: "Marcus",
IsPublished: true,
IsDeleted: false,
Slug: null,
Url: null,
SortOrder: 0
},
Parent: {
id: "pages/293",
slug: "slug",
url: "url"
},
Children: [
"pages/226",
"pages/257"
]
},
ParentPage: {
Heading: "Parent",
Id: "pages/293",
Metadata: {
Name: "Parent",
Title: null,
Keywords: null,
Description: null,
DisplayInMenu: true,
Published: "/Date(1334499539404)/",
Changed: "/Date(1334499539404)/",
ChangedBy: "Marcus",
IsPublished: true,
IsDeleted: false,
Slug: "slug",
Url: "url",
SortOrder: 0
},
Parent: null,
Children: []
},
Children: [
{
Heading: "Foo",
MainBody: null,
Id: "pages/226",
Metadata: {
Name: "I'm an article",
Title: null,
Keywords: null,
Description: null,
DisplayInMenu: true,
Published: "/Date(1334511318838)/",
Changed: "/Date(1334511318838)/",
ChangedBy: "Marcus",
IsPublished: true,
IsDeleted: false,
Slug: "i-m-an-article",
Url: "i-m-an-article",
SortOrder: 1
},
Parent: {},
Children: [ ]
},
{
Heading: "Bar",
MainBody: null,
Id: "pages/257",
Metadata: {
Name: "Foo",
Title: null,
Keywords: null,
Description: null,
DisplayInMenu: true,
Published: "/Date(1334953500167)/",
Changed: "/Date(1334953500167)/",
ChangedBy: "Marcus",
IsPublished: true,
IsDeleted: false,
Slug: "foo",
Url: "foo",
SortOrder: 2
},
Parent: {},
Children: [ ]
}
]
}
在查看上述响应时,我确信答案是将这些数据合并到我的控制器中,而不是由它们自己获取对象和集合,但我仍然对第二种意见感到好奇。