views:

47

answers:

2

I have an application that will support multiple sites. The site will be determined based on the url.

For example http://myapp/site/abc123/... and http://myapp/site/xyz123/...

The site code will drive a lot of the functionality for example themes, available modules, etc...

Questions:

1-)I need to validate the site code is valid and if it isn't, it should direct the user to an info page. I was looking at using IRouteConstraint, is this appropriate? Are there other/better options?

2-)Any gotchas with this approach (using url to identify site)? Is there are better approach?

Solution

I ended up creating a Custom ActionFilter and check the sitecode in the OnActionExecuting event. That seems to work well and fit better than the IRouteConstraint.

+2  A: 

The system I have implemented uses Urls to identify unique page content within a single site and the routing process is pretty straightforward. That being said, you may want to consider making use of Areas in your MVC application. With Areas you can have multiple sections to your website that all have their own MVC structure which can run semi-independently.

Essentially, you will have one base routing definition that lays out some defaults and then the rest of the "sites" will define their own routes pointing to controllers and views in a separate location. It's pretty easy to set up, you'll just need to make sure you're using version 2.0 of ASP.NET MVC. Here's a decent looking tutorial on ASP.NET MVC Areas and Routes. In the current model which MVC 2.0 supports you'll have a single Web project for each area, but that is not necessarily a requirement. Phil Haacked has some code for ASP.NET MVC Single Project Areas if you're looking for another example of the technique, although you, personally, will probably benefit more from the first article.

So long as you define good routes that have clear and measurable constraints, you shouldn't have too much trouble laying out the website you've described.

Nathan Taylor
For your system, did you use IRouteConstraint or a custom Filter? After playing with both, I think a custom filter fits better.
B Z
I did not need to use either, I was able to achieve what I needed with just standard routing rules.
Nathan Taylor
Ok, how did you validate if the id (site code in my example) was correct?
B Z
IF the format of the site code is consistent then you could use a regular expression condition in the routing rule to ensure it is valid. What is the format of the site code?
Nathan Taylor
I understand the formatting part using a regular expression. But that doesn't mean the actual site code is valid since it needs to be validated against a datastore. I used a customactionfilter and check on the onactionexecuting method, it seems to work well.
B Z
A: 

I ended up creating a Custom ActionFilter and check the sitecode in the OnActionExecuting event. That seems to work well and fit better than the IRouteConstraint.

B Z