如何限制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