Guldager
Guldager

Reputation: 13

How to write a list in the URL and pass it to an MVC controller in ASP.NET

I need to be able to write a list in an URL that is mapped to a controller which then does something on the list. As an example:

i go to http://localhost:8080/API/ControllerID/Action/a,b,c,d

which i would like to parse to a controller that looks like:

public ActionResult Action(List<string> ListItems)
{
    // do something on the list
}

And the maproute looking somthing like this:

context.MapRoute(
     "ListActions",
     "API/{controller}/{action}/{ListItems}",
     new { controller = "", action = "", ListItems = ""}
);

Is this possible?

Upvotes: 0

Views: 899

Answers (2)

VahidN
VahidN

Reputation: 19156

And don't do that, Plz! From security perspective having or letting long URL's is not safe at all + IIS will/can restrict that.

Upvotes: 0

Johnny_D
Johnny_D

Reputation: 4652

Yes, possible. Use different delimeter, e.g. "-", whatever doesn't break logic. in controller you can split string via String.Split.

Upvotes: 2

Related Questions