mercredi 5 juin 2019

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.

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.

My route at the moment is [Route("tenants/{tenantId:long}/subscriptions/{subscriptionId:long}/invoices/{invoiceId:long}/categories")]

From this way it seems impossible for me to pass more subscription IDs. Some terms I found but not fully understand are:

  • Model Binding
  • [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")]

    [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).
  • The currently written code is for a specific subscription.

I am looking for a Route like /api/invoices?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.




Aucun commentaire:

Enregistrer un commentaire