我会尽可能多地欣赏信息,因为我还是新手;)
我的控制器:
//GET: API/Creditors public I@R_419_5962@able<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","RegisteredForGST":true}]
这是我想要的结果(只有“CreditorID”和“Businessname”):
[{"CreditorID":1,"Businessname":"test1"},"Businessname":"test2"}]解决方法 在您的问题中,您正在显示查询的Json输出,因此我假设您正在从JavaScript发出GET请求.当您使用I@R_419_5962@able作为API方法的返回值类型时,您应该能够利用WebAPI提供的OData支持,以便您可以发出OData查询以仅选择所需的列.这个 this article有关OData支持的更多细节.
首先,JavaScript方面,假设j@R_419_5962@易于回答:
$.get('API/Creditors?$select=CreditorID,Businessname',onSuccess)
所需的列名称在$select参数的逗号分隔列表中指定. (onSuccess是一个你将定义的回调函数,它将传递从API返回的数据.有关详细信息,请参阅jQuery documentation.)
在服务器端,您可能需要从ODataController而不是APIController派生控制器,您需要将[@R_419_5962@able]或[Enable@R_419_5962@]属性添加到GetCreditors()方法,具体取决于您使用的WebAPI版本.
如果您发现需要从ODataController继承以使其工作,那么还需要添加一些配置,即配置OData端点.为此,您将需要类似于以下内容的代码:
public static class WebAPIConfig{ public static voID Register(httpConfiguration config) { ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet<Creditor>("Creditors"); config.MapODataServiceRoute( routename: "ODaTaroute",routePrefix: null,// or "API" ? model: builder.GetEdmModel()); }}
在您的Web启动代码中的某处(例如Application_Start),您需要按如下方式调用它:
GlobalConfiguration.Configure(WebAPIConfig.Register);
根据你如何设置你的项目,后面的一些配置可能没有必要,因为它已经完成,但我想我会提到它的好处.有关详细信息,请查看this page.
总结以上是内存溢出为你收集整理的entity-framework – 如何限制web api返回的列?全部内容,希望文章能够帮你解决entity-framework – 如何限制web api返回的列?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)