I am working on a simple MVC2 multi-tenant application. My question is how do I determine which tenant portal has been asked for in the url by the user? What I need to have happen is this:
- A request to http://localhost should go to the standard Home controller’s index page
- A request to http://localhost开发者_开发技巧/client1 should go to the ClientPortalHome controller’s index page
- A request to http://localhost/client1/LogOn will go to the client specific logon page
The two routes below achieve this and seem to work fine.
routes.MapRoute(
"Client Portal Default", // Route name
"{clientportal}/{controller}/{action}/{id}", // URL with parameters
new { controller = "ClientPortalHome", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults,
);
My question is how do I determine which client portal has been asked for (client1 in the above example)?
I tried using this:
private void Application_BeginRequest(Object source, EventArgs e)
{
var route = RouteTable.Routes.GetRouteData(new HttpContextWrapper(Context));
var currentPortal = route.GetRequiredString("clientportal");
Context.Items.Add("ClientPortalName", currentPortal);
}
The problem is that it is called for all routes and throws an exception for the Home controller case because the route does not contain a “clientportal” entry.
What’s the best way to determine which portal has been requested so I can make sure it exists and show the client specific home page? Do I need a custom route handler?
Thank you in advance.
Rick
This should work:
private void Application_BeginRequest(Object source, EventArgs e)
{
var route = RouteTable.Routes.GetRouteData(new HttpContextWrapper(Context));
object currentPortal;
if(route.Values.TryGetValue("clientportal", out currentPortal))
{
Context.Items.Add("ClientPortalName", currentPortal);
}
}
What do you guys think about about a route handler? What are the pros and cons of this approach vs. Application_BeginRequest?
public class PortalRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var portalName = requestContext.RouteData.GetRequiredString("clientportal");
if (!string.IsNullOrWhiteSpace(portalName))
{
// Grab the portal details from the database
var portalEntity =
Repository.For<ClientPortalEntity>().GetAll().Where(x => x.PortalTag == portalName).First();
if (portalEntity != null)
{
// Register db object for use by controllers
IoC.Container.RegisterInstance<ClientPortalEntity>(portalEntity);
}
}
// Return the default MVC HTTP handler for the configured request
return new MvcHandler(requestContext);
}
}
精彩评论