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.