[英]How to pass multiple parameters to Get Method and Route them in .NET Core Web API?
I'm making a (restful) Web API in .NET Core and stumbled among some problems. 我在.NET Core中创建了一个(宁静的)Web API,并且遇到了一些问题。
I cannot seem to find how to pass multiple subscription ID's... I need to be able to show multiple periods(invoices) of multiple subscriptions. 我似乎无法找到如何传递多个订阅ID ... 我需要能够显示多个订阅的多个期间(发票)。
My route at the moment is [Route("tenants/{tenantId:long}/subscriptions/{subscriptionId:long}/invoices/{invoiceId:long}/categories")] 我目前的路线是[路线(“租户/ {tenantId:long} / subscriptions / {subscriptionId:long} / invoices / {invoiceId:long} / categories”)]
From this way it seems impossible for me to pass more subscription IDs. 从这个角度来看,我似乎无法传递更多的订阅ID。 Some terms I found but not fully understand are:
我发现但未完全理解的一些术语是:
My classes: 我的课程:
public class Subscription
{
public string Name { get; set; }
public long TenantId { get; set; }
public string Guid { get; set; }
}
public class Invoice
{
public long SubscriptionId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public long PortalId { get; set; }
}
My controllers with routes [Route("tenants/{tenantId:long}/subscriptions")]
and [Route("tenants/{tenantId:long}/subscriptions/{subscriptionId:long}/invoices")]
我的控制器有路由
[Route("tenants/{tenantId:long}/subscriptions")]
和[Route("tenants/{tenantId:long}/subscriptions/{subscriptionId:long}/invoices")]
[HttpGet]
public IEnumerable<SubscriptionViewModel> Find(long tenantId)
{
var subscriptionList = _subscriptionManager.Find(tenantId);
...
return subscriptionViewModels;
}
[HttpGet]
public IEnumerable<InvoiceViewModel> Find(long subscriptionId)
{
var invoiceList = _invoiceManager.Find(subscriptionId);
...
return invoiceViewModels;
}
I am looking for a Route like /api/invoices?subscriptionId=x,y,z 我正在寻找/ api /发票之类的路线?subscriptionId = x,y,z
I understand(?) I need the [FromQuery] for that, but I cannot seem to find out how, especially if my parameter (subscriptionId) stays the same. 我理解(?)我需要[FromQuery],但我似乎无法弄清楚如何,特别是如果我的参数(subscriptionId)保持不变。
for the requirement which you have mentioned as: 对于您提到的要求:
I am looking for a Route like /api/invoices?subscriptionId=x,y,z
我正在寻找/ api /发票之类的路线?subscriptionId = x,y,z
You can do couple of things: 你可以做几件事:
&
in the query string of the URL and change the input parameter of action method to accept array of subscriptionIds example of route: &
传递subscriptionIds,并将action方法的输入参数更改为接受subscriptionIds数组的路由示例: /api/invoices/find?subscriptionId=x&subscriptionId=y&subscriptionId=z
example of action method parameter accepting array of subscriptionIds: action方法参数接受subscriptionIds数组的示例:
public IEnumerable<InvoiceViewModel> Find([FromQuery]long[] subscriptionId)
/api/invoices/find?subscriptionIds=x,y,z
example of action method: 动作方法示例:
public IEnumerable<InvoiceViewModel> Find([FromQuery]string subscriptionIds)
{
var ids = subscriptionIds.Split(',').Select(int.Parse).ToArray();
// do the logic on multiple subscriptionIds
}
Apart from this, you can go for creating custom model binders as well as suggested in other answers. 除此之外,您还可以创建自定义模型绑定器以及其他答案中的建议。
Hope this helps. 希望这可以帮助。
You can create a specific Request view model which accepts a collection of invoice ids: 您可以创建一个特定的请求视图模型,该模型接受发票ID的集合:
public class InvoiceRequestModel
{
IEnumerable<long> InvoiceIDS { get; set; }
}
and use it for your action method: 并将其用于您的操作方法:
[Route("tenants/{tenantId:long}/subscriptions/{subscriptionId:long}/invoices")]
[HttpGet]
public IEnumerable<InvoiceViewModel> Get(InvoiceRequestModel requestModel)
{
}
In the case you want to use query parameters, mark your action parameter with the [FromQuery]
attribute: 如果要使用查询参数,请使用
[FromQuery]
属性标记action参数:
[Route("tenants/{tenantId:long}/subscriptions/{subscriptionId:long}/invoices")]
[HttpGet]
public IEnumerable<InvoiceViewModel> Get([FromQuery]IEnumerable<long> invoiceIDs)
{
}
and on creating the request, pass each value with the same key in the query string: 在创建请求时,使用查询字符串中的相同键传递每个值:
invoiceIDs=1&invoiceIDs=2&invoiceIDs=3
Finally, it will look like this: 最后,它看起来像这样:
tenants/{tenantId}/subscriptions/{subscriptionId}/invoices?invoiceIDs=1&invoiceIDs=2&invoiceIDs=3
There can be many ways to achieve this task (I can think of two-three for now). 有很多方法可以完成这项任务(我现在可以想到两到三个)。
1) instead of long subscriptionid take a string as an input and validate it before proceeding further. 1)而不是long subscriptionid将字符串作为输入并在继续进行之前对其进行验证。
[HttpGet]
public IEnumerable<InvoiceViewModel> Find(string subscriptionIds)
{
var list = validateInput(subscriptionIds);
var invoiceList = _invoiceManager.FindList(list);
...
return invoiceViewModels;
}
public IList<long> validateInput(string subscriptionIds)
{
var list = subscriptionIds.Split(",");
... // Code to convert each element in long and throw if it is not long
return longlist;
}
2) Create custom model binders. 2)创建自定义模型绑定器。
Steps are mentioned here: 这里提到的步骤:
https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
=> [FromUri] attribute can be used to bind the Complex types from query string parameters but i am not sure how i would use that. => [FromUri]属性可用于从查询字符串参数绑定复杂类型,但我不知道我将如何使用它。
If you ask me, i would go for approach-1 (not to increase complexity). 如果你问我,我会选择接近-1(不增加复杂性)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.