简体   繁体   English

将参数传递给ASP.NET的Web API 2 OData v4 REST API

[英]Passing parameters to ASP.NET's Web API 2 OData v4 REST API

Microsoft's OData implementation lets filtering a dataset by applying a boolean expression in the $filter option. Microsoft的OData实现允许通过在$ filter选项中应用布尔表达式来过滤数据集。 This serves well for dynamic filtering (usually requested by the client), but what if the REST API's GET method for enumerating a list needs some base input parameters that are always required? 这适用于动态过滤(通常由客户端请求),但是如果用于枚举列表的REST API的GET方法需要一些始终需要的基本输入参数,该怎么办?

For example, let's define the following models on our Web API controller: 例如,让我们在Web API控制器上定义以下模型:

public class RoastedCoffeeProduct
{
    [Key]
    public Guid ProductId { get; set; }
    public string Name { get; set; }
    public string Brand { get; set; }
    public string StoreName { get; set; }
    public Location StoreLocation { get; set; }
    public decimal CurrentPrice { get; set; }
    public int StoreStock { get; set; }
}

public class Location
{
    public Guid LocationId { get; set; }
    public string Country { get; set; }
    public string Region { get; set; }
}

and then define the following GET method on the REST API: 然后在REST API上定义以下GET方法:

/odata/RoastedCoffeeProducts/

The expectation is to get an enumeration of all roasted coffee products available for sale in the stores of a given location. 期望在特定地点的商店中列出可供出售的所有烘焙咖啡产品。 A use case would be the following: "Get all roasted coffee products for sale on location Country: 'US', Region: 'Redmond'". 一个用例如下:“将所有烘焙咖啡产品出售到位置国家:'美国',地区:'雷德蒙德'”。 This should return an enumeration of many JSON items like the one below (only one is shown for brevity): 这应该返回许多JSON项的枚举,如下所示(为简洁起见,只显示一个):

{
   "ProductId": "D6B543AE-D6A6-42C2-A4B1-70402B4B2AD1",
   "Name": "Iperespresso capsules",
   "Brand": "Illy",
   "StoreName": "5 Stones Coffee Company",
   "StoreLocation": { "LocationId": "0AB30128-0EF1-4B8C-9EC6-DCF0F27BB828", "Country": "US", "Region": "Redmond" },
   "CurrentPrice": "15.90",
   "StoreStock": "4"
}

So let's assume that the required base input parameters for the GET method are "Country" and "Region". 因此,我们假设GET方法所需的基本输入参数是“Country”和“Region”。 They always should be provided, because the Web API 2 Controller calls the following method for fetching them: 应始终提供它们,因为Web API 2 Controller调用以下方法来获取它们:

var productsBeingSold = storesRepository.FindAllCoffeeProducts(country, region);

So, the FindAllCoffeeProducts method in the repository needs the country and region as input parameters. 因此,存储库中的FindAllCoffeeProducts方法需要国家和地区作为输入参数。 Let's assume this method returns about 200 items. 我们假设这个方法返回大约200个项目。 If the client wants to further filter this 200 items dataset, like, those whose product name is "Nespresso capsules", then OData filtering capabilities enter in action: 如果客户想要进一步过滤这200个项目数据集,例如那些产品名称为“Nespresso capsule”的数据集,那么OData过滤功能将进入操作:

/odata/RoastedCoffeeProducts?$filter=name eq 'Nespresso capsules'

That way OData will automatically filter all matching items from the 200 items dataset. 这样,OData将自动过滤200项数据集中的所有匹配项。 My question is about the required base input parameters Country and Region: should they be provided in the OData $filter clause as well? 我的问题是关于所需的基本输入参数Country和Region:它们是否也应该在OData $ filter子句中提供? I think this is not correct, because strictly speaking is not part of the filter, but part of the input parameters needed to work properly. 我认为这是不正确的,因为严格来说不是过滤器的一部分,而是正常工作所需的输入参数的一部分。 Where should they be provided then? 那么他们应该在哪里提供? In the body maybe? 在体内也许? OData filtering should be applied to the resulting dataset, but the dataset needs to know the Country and Region as required input parameters. OData过滤应该应用于结果数据集,但数据集需要将Country和Region知道为必需的输入参数。

What you are describing is an OData Function, this can take certain parameters and the results can still be filtered and sorted etc. For more details see this link: https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-and-functions 您所描述的是一个OData函数,它可以采用某些参数,结果仍然可以过滤和排序等。有关详细信息,请参阅此链接: https//docs.microsoft.com/en-us/aspnet/web- API /概述/ OData的支持功能于ASPNET-Web的API /的OData-V4 / OData兼容的行为,和功能

Here is an example of an unbound function using the OData TripPin example service that has a function called GetNearestAirport that takes 2 parameters, lat and lon that have been specified in the URL: http://services.odata.org/V4/TripPinServiceRW/GetNearestAirport(lat=0,lon=0) 下面是一个使用OData TripPin示例服务的未绑定函数的示例,该服务具有一个名为GetNearestAirport的函数,该函数接受已在URL中指定的2个参数latlonhttp//services.odata.org/V4/TripPinServiceRW/ GetNearestAirport(LAT = 0,LON = 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM