Andre Pena
Andre Pena

Reputation: 59336

Is there a way I can implement a custom route so that certain route values are automatically passed?

I have an application that is multitenant, that is, I have multiple clients that use the application and share a single domain using folders. Like the following:

  1. www.mydomain.com/client1/home/index
  2. www.mydomain.com/client2/home/index

I can't use areas because the clients are not static, they register and they must be able to start using the application right away. I can't use subdomains because in this case I don't have dynamic control over the DNS.

That being said, how can I implement a custom route so that I don't need to pass the client name every time I build an outgoing URL?

That is, I want this to be true:

@Url.Action("Index", "Home") -> www.mydomain.com/client1/home/index

without passing client1, something can resolve client1 because by the time I build this URL, this string will be a value of the current route values, because I'm already at the client1

How can I do that?

Upvotes: 1

Views: 67

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

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

Now when you navigate to /client1/Home/Index and if inside the Index.cshtml view when you use:

@Html.ActionLink("foo", "About")

this will generate:

<a href="/client1/home/About">foo</a>

Same stands true for using the Url.Action helper inside the Index action. It will preserve the client route value from the original request.

Upvotes: 5

Related Questions