如何限制web api &实体框架返回的列? 因为我仍然是一个新手,我希望尽可能多的信息成为可能;)如何限制web api返回的列?
我的控制器:
//GET: api/Creditors
public IQueryable<Creditor> GetCreditors()
{
return db.Creditors;
}
我的班级:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace PurchaseOrders.Models
{
public class Creditor
{
[Key]
public int CreditorID { get; set; }
[MaxLength(10, ErrorMessage = "Maximum of 10 characters")]
public string CRKEY { get; set; }
[Display(Name = "Business Name")]
[MaxLength(40, ErrorMessage = "Maximum of 40 characters")]
public string BusinessName { get; set; }
[MaxLength(40, ErrorMessage = "Maximum of 40 characters")]
public string Address { get; set; }
[MaxLength(40, ErrorMessage = "Maximum of 40 characters")]
public string City { get; set; }
[MaxLength(4, ErrorMessage = "Maximum of 4 characters")]
public string State { get; set; }
[MaxLength(4, ErrorMessage = "Maximum of 4 characters")]
public string Postcode { get; set; }
[MaxLength(15, ErrorMessage = "Maximum of 15 characters")]
public string Phone { get; set; }
[MaxLength(15, ErrorMessage = "Maximum of 15 characters")]
public string Fax { get; set; }
[MaxLength(60, ErrorMessage = "Maximum of 60 characters")]
public string Email { get; set; }
[MaxLength(60, ErrorMessage = "Maximum of 60 characters")]
public string Website { get; set; }
[MaxLength(30, ErrorMessage = "Maximum of 30 characters")]
public string ContactName { get; set; }
[MaxLength(15, ErrorMessage = "Maximum of 15 characters")]
public string ABN { get; set; }
[Display(Name = "Registered for GST")]
public bool RegisteredForGST { get; set; }
}
}
这个当前返回:
[{"CreditorID":1,"CRKEY":"test1","BusinessName":"test1","Address":"7 Smith Street","City":"Melbourne","State":"VIC","Postcode":"3000","Phone":null,"Fax":null,"Email":null,"Website":null,"ContactName":null,"ABN":"null","RegisteredForGST":true},{"CreditorID":2,"CRKEY":"test2","BusinessName":"test2","Address":"10 Smith Street","City":"SYDNEY","State":"NSW","Postcode":"2000","Phone":null,"Fax":null,"Email":null,"Website":null,"ContactName":null,"ABN":"null","RegisteredForGST":true}]
这是我想要的结果(只有“CreditorID”&“BUSINESSNAME”):
[{"CreditorID":1,"BusinessName":"test1"},{"CreditorID":2,"BusinessName":"test2"}]
我一直在玩odata&看起来不错..它和webapi一样快吗?你知道它是否会在vnext中得到支持吗? – MWD 2014-10-21 15:06:06
我不确定,但我期望。团队在此页面发表的评论(http://blogs.msdn.com/b/webdev/archive/2014/09/11/announcing-the-release-of-web-api-odata-5-3.aspx )表示当vNext处于“稳定”状态时,他们会查看“如何启用它”。那是在9月11日,所以事情可能会继续。它正在积极发展,所以我会想象它们将会移植。至于速度,我还没有看到任何速度问题。事实上,事实上正好相反,因为我已经设法对数据库进行更有针对性的查询了。 – 2014-10-21 16:00:19
感谢Josh ..我在玩了一天的odata后觉得很印象深刻,并且认为我会尽我所能开始使用它;) – MWD 2014-10-21 20:14:12