0

In ASP.Net MVC, is it possible to define to a route that there may be an unknown number of arguments? For example assuming the following code:

public void Sample(params String[] args) {}

Can we mimic the same concept in a route like [Route("Product/{id}")] so that we get the following result working:

 /Product/PID_01/PID_02/PID_03,....
Arnold Zahrneinder
  • 4,788
  • 10
  • 40
  • 76
  • 2
    You could have a catch-all route - refer [these answers](http://stackoverflow.com/questions/7515644/infinite-url-parameters-for-asp-net-mvc-route) –  Apr 30 '17 at 12:27
  • @StephenMuecke: If we have more arguments like `Product/{Group}/{id}` then if catching all arguments, how can you tell which is which. I mean would the param argument be represented in form an array? – Arnold Zahrneinder Apr 30 '17 at 12:29

1 Answers1

2

Yes, it's possible using catch-all route:

[Route("foo/{group}/{*id}")]
public IActionResult Foo(string group, string id)
{
    var ids = id.Split('/');
    // Do your stuff
}

Then go to:

http://yourapp/foo/Product/PID_01/PID_02/PID_03

You can also define a custom ModelBinder to avoid the splitting in the controller code.

rocky
  • 7,506
  • 3
  • 33
  • 48