How to create virtual domains 3 levels in ASP.NET MVC 3?

I want to register in the system, each user created its own page on a domain 3 view level

How to do that in ASP.NET MVC 3?

Let this be a virtual domain, which is then interpreted by the system as
October 8th 19 at 03:41
3 answers
October 8th 19 at 03:43
Maybe this will help.
thank you! Here is another article on the subject found - Jayde.Tow commented on October 8th 19 at 03:46
October 8th 19 at 03:45
I did so: routes added one parameter (domain).

 new { domain = "", controller = "Home", action = "Index", id = "" }

Is $ substitute any symbol that You are not going to use in the url. Thus we achieve the uniqueness of the ID domain. Then, using the URL Rewrite Module for IIS 7, set rewrite rules so that requests for subdomains to the main domain redirections, in accordance with the above route (to be added before the route $). I did it through the GUI in the admin interface of IIS, so the code can not provide.
Just wondering where in IIS this is prescribed! If You don't mind, can you make a screenshot of this window and what to prescribe - Jayde.Tow commented on October 8th 19 at 03:48
Yes, and since $ is not entirely clear. It is necessary to use? For what? What if without it, the route to prescribe? - Jayde.Tow commented on October 8th 19 at 03:51
$ need for uniqueness. A small example:
'' => rewrite => '$domain' will be treated as '$domain/HomeController/IndexAction'. We can exactly identify a request to a subdomain thanks to the $. Now suppose there is another route:
 new { controller = "Other" action = "Index" }

Request ''

If you do not use $, as in this case to identify a 'text'? How overwritten a request for a subdomain or as a direct request id?

Although, if you have the routes do not intersect or explicitly (without dynamically determine the controller and action) you can do without $. - Jayde.Tow commented on October 8th 19 at 03:54
A screenshot can not provide. It was a year ago. Now I'm working under linux. - eva_Kemmer18 commented on October 8th 19 at 03:57
October 8th 19 at 03:47
You have to write your own handler routing.
I used that option. Original version found on one of the forums and modified it a bit.
public class SubDomainRoute : Route
 private Regex domainRegex;
 private Regex pathRegex;
 private readonly string[] namespaces;
 public string Domain { get; set; }

 public SubDomainRoute(string domain, string url, object defaults, string[] namespaces)
 : base(url, new RouteValueDictionary(defaults), new MvcRouteHandler())
 Domain = domain;
 this.namespaces = namespaces;

 public override RouteData GetRouteData(HttpContextBase httpContext)
 // Build regex
 domainRegex = CreateRegex(Domain);
 pathRegex = CreateRegex(Url);

 // Request information
 requestDomain string = httpContext.Request.Headers["host"];
 if (!string.IsNullOrEmpty(requestDomain))
 if (requestDomain.IndexOf(":") > 0)
 requestDomain = requestDomain.Substring(0, requestDomain.IndexOf(":"));
 requestDomain = httpContext.Request.Url.Host;
 string requestPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo;

 // Match domain and route
 Match you = domainRegex.Match(requestDomain);
 Match pathMatch = pathRegex.Match(requestPath);

 if (you.Success && pathMatch.Success)
 var routeData = base.GetRouteData(httpContext);

 if (this.namespaces != null && this.namespaces.Length > 0)
 routeData.DataTokens["Namespaces"] = this.namespaces;
 //then add the field {host} to the routes
 routeData.DataTokens["Host"] = routeData.Values["host"];
 routeData.DataTokens["UseNamespaceFallback"] = bool.FalseString;

 return routeData;

 return null;

 public override virtualpathdata an GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)

 return base.GetVirtualPath(requestContext, values);
 // return null;

 CreateRegex private Regex(string source)
 // Perform replacements
 source = source.Replace("/", @"\/?");
 source = source.Replace(".", @"\.?");
 source = source.Replace("-", @"\-?");
 source = source.Replace("{", @"(?<");
 source = source.Replace("}", @">([a-zA-Z0-9_]*))");

 return new Regex("^" + source + "$");

And then describe the route like this:
routes.Add("host_default," new SubDomainRoute(
 new { controller = "User", action = "Index", id = UrlParameter.Optional },
 new[] { typeof(Controllers.UserController).Namespace } // Namespaces defaults

We still have in the User controller to check the validity of the subdomain.

Find more questions by tags ASP.NET