gpt4 book ai didi

entity-framework - 如何限制 web api 返回的列?

转载 作者:行者123 更新时间:2023-12-01 02:14:11 25 4
gpt4 key购买 nike

如何限制 Web api 和 Entity Framework 返回的列?
由于我仍然是新手,我会尽可能多地了解信息;)

我的 Controller :

         //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"}]

最佳答案

在您的问题中,您显示的是查询的 json 输出,因此我假设您是从 Javascript 发出 GET 请求。当您使用 IQueryable作为 API 方法返回值的类型,您应该能够利用 WebApi 提供的 OData 支持,以便您可以发出 OData 查询以仅选择所需的列。此 this article有关 OData 支持的更多详细信息。

所以首先是 javascript 方面,假设 jQuery 为便于回答:

$.get('api/Creditors?$select=CreditorId,BusinessName', onSuccess)

您想要的列名在 $select 中以逗号分隔的列表中指定。争论。 ( onSuccess 是您将定义的回调函数,它将传递从 API 返回的数据。有关更多详细信息,请参阅 jQuery documentation。)

在服务器端,您可能需要从 ODataController 派生您的 Controller 。而不是 ApiController您需要添加 [Queryable][EnableQuery]归因于您的 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());
}
}

在您的网络启动代码(例如 Application_Start )中的某处,您需要按如下方式调用它:
GlobalConfiguration.Configure(WebApiConfig.Register);

根据您设置项目的方式,后一种配置中的一些可能不是必需的,因为它已经完成,但我想我会提到它是很好的衡量标准。看看 this page更多细节。

关于entity-framework - 如何限制 web api 返回的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26437859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com