ASP.NET Core MVC : actionlink parameter to use slashes (/) insted of questionmark (?) in a query string

45 views Asked by At

I would like to map a route that would be valid for any number of parameter passed as a query string for any controller and methods.

For instance:

/Area/Controller/Action?paraA=1&paraB=2 

should be changed to

/Area/Controller/Action/paraA/1/paraB/2

The standard mapping only works for an id parameter. I want to update it so that it works for any number of parameters. Any suggestions?

Standard implementation is

app.MapControllerRoute(
    name: "MyArea",
    pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
1

There are 1 answers

0
Brando Zhang On

According to your description, the asp.net core route template contains the a catch-all parameter.

You could put it like below:

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{*ids}");

If you put the /Area/Controller/Action/1/2/3/4 inside the url, the controller method will capture it like below:

enter image description here

Then you could use Split method to split it and use it.