views:

52

answers:

2

I'm developing an ASP.NET MVC multi site application (can host multiple sites from the same application instance / multi-tenant application).

I've previously done this in a web forms application and loaded up the appropriate site configuration data (by inspecting the url) on the page_load event of a custom base page.

With ASP.NET MVC would I be best to do the same in Application_BeginRequest? I can then add the configuration object to HttpContext.Current.Items.

+2  A: 

This blogger has what looks to be a decent series of articles about multi-tenancy in asp.net-mvc

What you mention sounds like a workable way of doing what you want, though it may not be the idiomatic solution.

BioBuckyBall
Thanks for the link - looks like it covers just what I need.
Ben
No problem...saved it for myself as well :)
BioBuckyBall
+1  A: 

I'm doing something similar with the current sytsem I'm working on.

I'm determinging the site by the url the user accesses the application by, as follows:

public class BaseController : Controller
{
    protected override void Initialize(RequestContext requestContext)
    {
        var host = requestContext.HttpContext.Request.Url.Host;
        Site = _siteService.GetSiteByHost(host);

        base.Initialize(requestContext);
    }       
}

every Controller in my system extends BaseController, so the Site object is available for every Controller. If you have any more specific questions, ask and I'll let you know what we did.

upate

what you see above is _siteService.GetSiteByHost(host). SiteService has a caching layer between it and the Repository, which takes care of all the cache related stuff. it is defined as:

    public Site GetSiteByHost(string host)
    {
        string rawKey = GetCacheKey(string.Format("GetSiteByHost by host{0}", host));

        var site = (Site)_cachingService.GetCacheItem(rawKey);

        if (site == null)
        {
            site = _siteRepository.GetSiteByHost(host);
            AddCacheItem(rawKey, site);
        }

        return site;
    }

We try not to use the Sesson unless we're dealing with simple data types, like an int. I wouldn't store a Site object in the Session, but i would store an int that represents the SiteId in the session. Then the next time the user accesses the application, I can get the Site object that's associated with the current user from the cache by Id. In the case above though that's not necessary.

DaveDev
Dave, one question - I assume that when you caching items or using the session object, you are prefixing your keys with the site identifier?
Ben
I updated the answer to reflect your question.
DaveDev
Great, thanks - this was helpful
Ben
Would also like to share this link as it's kind of related - discussing new .NET 4 System.Runtime.Cache and example of rolling own cache provider - http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-ASPNET-4-and-the-new-ObjectCache-with-Stefan/
Ben
No problem man, but if either of these answers (or any others on StackOverflow) are correct, make sure to mark it as such!! That's what gives this site its value
DaveDev
@DaveDev - I've marked yours as the answer as it was more specific to my question. The link by @Lucas was also very useful.
Ben