patterncsharpMinor
"Pluggable" routes in ASP.NET MVC
Viewed 0 times
routesmvcpluggablenetasp
Problem
How do I handle multi-tenancy with routes in ASP.NET MVC?
The application is a multitenant ASP.NET MVC 3 application. Tenants are identified based on hostname so this does not interfere with routing.
Other important information:
-
The application has a number of features (or plugins). These are essentially other web projects with their own controllers, views and routes.
-
ASP.NET MVC routing serves as our entry point into these features.
-
One key point is that all the features function the SAME way for all tenants. The variable is whether the tenant is actually subscribed to the feature.
So imagine we have a "blog" feature with a route like so:
When our application starts we register the routes of all "routable" features. However, we need to ensure that this route is only matched if the tenant is subscribed to that feature.
Currently I'm doing this with a custom route:
One thing that I need to cater for is routes that do not specify an instance.
For example, a tenant may have one or more blogs. If he only has one then outgoing routes s
The application is a multitenant ASP.NET MVC 3 application. Tenants are identified based on hostname so this does not interfere with routing.
Other important information:
-
The application has a number of features (or plugins). These are essentially other web projects with their own controllers, views and routes.
-
ASP.NET MVC routing serves as our entry point into these features.
-
One key point is that all the features function the SAME way for all tenants. The variable is whether the tenant is actually subscribed to the feature.
So imagine we have a "blog" feature with a route like so:
/blog/{instance}/postsWhen our application starts we register the routes of all "routable" features. However, we need to ensure that this route is only matched if the tenant is subscribed to that feature.
Currently I'm doing this with a custom route:
private readonly IFeatureMetadata feature;
public FeatureRoute(IFeatureMetadata feature, string url, RouteValueDictionary defaults, IRouteHandler handler) :
base(url, defaults, handler)
{
if (feature == null)
throw new ArgumentNullException("feature");
this.feature = feature;
}
public override RouteData GetRouteData(System.Web.HttpContextBase httpContext)
{
var routeData = base.GetRouteData(httpContext);
if (routeData == null)
return null;
var context = DependencyResolver.Current.GetService();
var site = context.GetSite();
var matchedInstances = site.FeatureInstances.Where(f => f.Feature.FeatureType == feature.FeatureType);
if (!matchedInstances.Any())
return null;
if (routeData.Values["instance"] == null)
{
routeData.Values["instance"] = matchedInstances.First().Id;
}
return routeData;
}One thing that I need to cater for is routes that do not specify an instance.
For example, a tenant may have one or more blogs. If he only has one then outgoing routes s
Solution
You could also create a route constraint that redirects or changes the routedata to "buy module".
http://msdn.microsoft.com/en-us/library/cc668201.aspx#adding_constraints_to_routes
http://www.jfarrell.net/2008/11/asp-net-mvc-authentication-strategies.html
http://msdn.microsoft.com/en-us/library/cc668201.aspx#adding_constraints_to_routes
http://www.jfarrell.net/2008/11/asp-net-mvc-authentication-strategies.html
Context
StackExchange Code Review Q#2393, answer score: 2
Revisions (0)
No revisions yet.