Matt Seymour
Matt Seymour

Reputation: 9395

MVC3 MapRoute, how to

I am looking at creating some new routes within my MVC3 application. What I want is a route that will allow me to produce:

{clientname}/{controller}/{action}/{id}

Where I am unsure is whether or not I should make use of the object defaults parameter.

Upvotes: 2

Views: 632

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could add the following route definition:

routes.MapRoute(
    "ClientRoute",
    "{clientname}/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Because clientname is at the beginning it is a compulsory value. It must always be specified and cannot be empty.

For example if you generate an anchor:

@Html.ActionLink("link text", "Foo", new { clientname = "bar" })

it would produce the following output:

<a href="/bar/Home/Foo">link text</a>

Upvotes: 4

Related Questions