0

Is it possible to pass multiple parameters to C# MVC action without knowing the exactly number?

For example, I could pass

mywebsite.com/brands/gm/ford/fiat/dodge

OR

mywebsite.com/brands/gm/ford/fiat

However, I don't want to have to create a route like:

"brands/{brand1}/{brand2}/{brand3}/{brand4}"

Because a variable number of brands will be passed to it.

How can I achieve this?

2 Answers 2

2

Technically, but only by using a wildcard param. Then, you're responsible for parsing the individual params out of the path yourself. For example:

routes.MapRoute(
    "Brands",
    "brands/{*brands}",
    new { controller = "Brands", action = "Index" }
);

Based on your first example URL, the value of the brands param would be the string "gm/ford/fiat/dodge". You'd then need to break this up to get the individual brands:

public ActionResult Index(string brands)
{
    var brandList = brands.Split("/");

    // do something with `brandList`
}
Sign up to request clarification or add additional context in comments.

Comments

1

Say you got a controller method:

public ActionResult Some(string brands)
{
    string[] individualBrands = brands.Split(';');

    return View();
}

The url would look like : http://localhost:50006/Home/Some?brands=brand1;brand2

3 Comments

In my case, I need for SEO purposes, slashed style.
Would look a bit weird though... definatly not standard... but that's a matter of taste really =)
@ramires.cabral: FWIW, what you're trying to achieve could actually work against you for SEO purposes. For example, /brands/gm/ford is a totally different URL from /brands/ford/gm, but your action would likely render the same content for both variants. This could result in duplicate content penalties if both URL forms get indexed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.