创建了一个输出 json 的 web api,尝试将它与backbone.js 分页插件一起使用,以将结果输出到backbone.js 无限分页插件
这是我输出的 json
[{"id":1,"title":"test1""desc":"book1"},
{"id":2,"title":"test2","desc":"book2"},
{"id":3,"title":"test3", "desc":"book3"},
{"id":4,"title":"test4","desc":"book4"},
{"id":5,"title":"test5","desc":"book5"},
{"id":6,"title":"test6","desc":"book6"}]
但我需要包含对象的名称,因为backbone.js 分页器需要返回响应对象。认为我几乎在那里,但似乎无法让它显示或弄清楚我如何将对象名称添加到它
{"object name:"[{"id":1,"title":"test1","desc":"book1"}, {"id":2,"title":"test2","desc":"book2"},
{"id":3,"title":"test3","desc":"book3"},
{"id":4,"title":"test4","desc":"book4"},
{"id":5,"title":"test5","desc":"book5"},
{"id":6,"title":"test6","desc":"book6"}]}
我的 .net api web api 看起来像这样
namespace newslist
{
public class NewsController : ApiController
{
List<News> Articles = new List<News>{
new News { id=1, title="test1", desc ="test1"},
new News { id=2, title="test2", desc ="test2"},
new News { id=3, title="test3", desc ="test3"},
new News { id=4, title="test4", desc ="test4"},
new News { id=5, title="test5", desc ="test5"},
new News { id=6, title="test6", desc ="test6"}
};
public List<News> GetAllLatestNews()
{
return Articles;
}
public News GetNewsById(int id)
{
var News = Articles.FirstOrDefault((p) => p.id == id);
if (News == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return News;
}
public IEnumerable<News> GetNewsByCategory(string desc)
{
return Articles.Where(
(p) => string.Equals(p.desc, desc,
StringComparison.OrdinalIgnoreCase));
}
}
}
类文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace newslist
{
public class News
{
public int id { get; set; }
public string title { get; set; }
public string desc { get; set; }
}
}