Subdomains in Asp.Net MVC Application
Helo Guys!
I'll start to develop a website of marketing and the users will be able to create accounts in this website. I'd like to separete the area of users using Subdomains, just like wordpress.com, ning.com, etc. If a user create an account for example: "john", the website should respond in john.mydomain.com for this user area, but it'll be the same code (separating my开发者_运维问答 subdomain).
I'd like to know, How can I do a code to identify the account (in subdomain) to show the correct account for the website ?
PS: Of course it isn't to be authenticated to show the website.
Thanks
My buddy and I get this one of our sites. We had a database table that held the SiteId, along with the Domain.
In a base controller, we parsed the domain when it came in, and then found the appropriate SiteId in the database. All the controllers inherited from this base controller, and the SiteId was passed though to each call to our model.
Admin.Site.com -> SiteId = 1
Frank.Site.com -> SiteId = 2
When someone hit Admin.Site.com
, SiteId of 1 was passed to the model to either retrieve the data, or save/edit/add/delete it.
Here is the code we used to get the domain:
public Website CurrentWebsite
{
get
{
if (_website == null)
{
var domain = HttpContext.Request.Url.Host;
_website = _websiteRepository.GetWebsiteByDomain(domain);
}
return _website;
}
}
The route handling in asp.net mvc starts after the domain name so it can not be caught using regular route handling.
You can create a route handler that adds the subdomain into the route data (taking it from the Request.Url) before the route is evaluated.
精彩评论