简体   繁体   English

如何将多个参数传递给Get方法并在.NET Core Web API中对它们进行路由?

[英]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: 我发现但未完全理解的一些术语是:

  • Model Binding 模型绑定
  • [FromQuery] [FromQuery]

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;
    }
  • Please note that i'm using a Mapper for my data (which is why i'm using ViewModels). 请注意我正在使用Mapper来获取我的数据(这就是我使用ViewModels的原因)。
  • The currently written code is for a specific subscription. 当前编写的代码用于特定订阅。

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: 你可以做几件事:

  1. pass the subscriptionIds one after the other separated by & in the query string of the URL and change the input parameter of action method to accept array of subscriptionIds example of route: 在URL的查询字符串中依次用&传递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)
  1. pass the comma separated string as querystring in the URL and write a piece of logic in the action method to split the string based on comma to get an array of subscriptionIds example of route: 在URL中将逗号分隔的字符串作为查询字符串传递,并在操作方法中写入一段逻辑,以基于逗号分割字符串,以获取routeIds的一个例子:route ::
/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.

相关问题 ASP.NET Core中如何将多个参数传递给一个get方法 - How to pass multiple parameters to a get method in ASP.NET Core 如何将参数传递给ASP.NET Web API Get方法? - how to pass parameters to asp.net web api get method? NET Core Web API中如何使用HTTP get方法传递参数? - how to pass a argument with HTTP get method in .net core web api? 如何将多个参数从 angular 8 传递到 .NET Core API - How to pass multiple parameters from angular 8 to .NET Core API 如何在asp.net WEB API中传递多个querystring参数 - how to pass multiple querystring parameters in asp.net WEB API .NET 6 - 如何使用可选路由参数 ASP.NET 核心 WEB ZDB974238714CA8DE634A7CE1D083A - .NET 6 - How to use Optional Route Parameters ASP.NET Core WEB API? .net 核心 web api - Z594C103F2C6E04C3D8AB059F031E0 可以传递参数到中间件吗? - .net core web api - Can controller pass parameters to middleware? 在ASP.NET Core Web Api中具有多个具有多个查询字符串参数的get方法 - Having multiple get-methods with multiple query string parameters in ASP.NET Core Web Api 如何将多个args传递/接收到RESTful Web API GET方法? - How to pass/receive multiple args to a RESTful Web API GET method? 如何使用ASP.NET Core Web API向url地址传递三个参数 - How to pass three parameters to url address using ASP.NET Core Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM