Create subdomain on the fly like blogger.com using asp.net mvc -


i want create sub domains on fly each of identity users (aspnetusers). can see kind of feature available in blogger.com.

i have list of sub domain names in database

users={"abc", "xyz","blah"}  

my original website this

http://mywebsite.com 

the above users should access website below

http://abc.mywebsite.com http://xyz.mywebsite.com http://blah.mywebsite.com 

i can in web.config above 3 users. users table change frequently. create sub domains on fly each user available in users table. how can achieve using asp.net mvc?

maarten balliauw has posted excellent solution downloadable demo issue titled asp.net mvc domain routing. note demo doesn't upgrade correctly recent versions of visual studio, there aren't many files required use method reverse engineering not painful.

domainroute.cs

using system; using system.collections.generic; using system.linq; using system.web; using system.web.routing; using system.web.mvc; using system.text.regularexpressions;  namespace mvcdomainrouting.code {     public class domainroute : route     {         private regex domainregex;         private regex pathregex;          public string domain { get; set; }          public domainroute(string domain, string url, routevaluedictionary defaults)             : base(url, defaults, new mvcroutehandler())         {             domain = domain;         }          public domainroute(string domain, string url, routevaluedictionary defaults, iroutehandler routehandler)             : base(url, defaults, routehandler)         {             domain = domain;         }          public domainroute(string domain, string url, object defaults)             : base(url, new routevaluedictionary(defaults), new mvcroutehandler())         {             domain = domain;         }          public domainroute(string domain, string url, object defaults, iroutehandler routehandler)             : base(url, new routevaluedictionary(defaults), routehandler)         {             domain = domain;         }          public override routedata getroutedata(httpcontextbase httpcontext)         {             // build regex             domainregex = createregex(domain);             pathregex = createregex(url);              // request information             string requestdomain = httpcontext.request.headers["host"];             if (!string.isnullorempty(requestdomain))             {                 if (requestdomain.indexof(":") > 0)                 {                     requestdomain = requestdomain.substring(0, requestdomain.indexof(":"));                 }             }             else             {                 requestdomain = httpcontext.request.url.host;             }             string requestpath = httpcontext.request.apprelativecurrentexecutionfilepath.substring(2) + httpcontext.request.pathinfo;              // match domain , route             match domainmatch = domainregex.match(requestdomain);             match pathmatch = pathregex.match(requestpath);              // route data             routedata data = null;             if (domainmatch.success && pathmatch.success)             {                 data = new routedata(this, routehandler);                  // add defaults first                 if (defaults != null)                 {                     foreach (keyvaluepair<string, object> item in defaults)                     {                         data.values[item.key] = item.value;                     }                 }                  // iterate matching domain groups                 (int = 1; < domainmatch.groups.count; i++)                 {                     group group = domainmatch.groups[i];                     if (group.success)                     {                         string key = domainregex.groupnamefromnumber(i);                          if (!string.isnullorempty(key) && !char.isnumber(key, 0))                         {                             if (!string.isnullorempty(group.value))                             {                                 data.values[key] = group.value;                             }                         }                     }                 }                  // iterate matching path groups                 (int = 1; < pathmatch.groups.count; i++)                 {                     group group = pathmatch.groups[i];                     if (group.success)                     {                         string key = pathregex.groupnamefromnumber(i);                          if (!string.isnullorempty(key) && !char.isnumber(key, 0))                         {                             if (!string.isnullorempty(group.value))                             {                                 data.values[key] = group.value;                             }                         }                     }                 }             }              return data;         }          public override virtualpathdata getvirtualpath(requestcontext requestcontext, routevaluedictionary values)         {             return base.getvirtualpath(requestcontext, removedomaintokens(values));         }          public domaindata getdomaindata(requestcontext requestcontext, routevaluedictionary values)         {             // build hostname             string hostname = domain;             foreach (keyvaluepair<string, object> pair in values)             {                 hostname = hostname.replace("{" + pair.key + "}", pair.value.tostring());             }              // return domain data             return new domaindata             {                 protocol = "http",                 hostname = hostname,                 fragment = ""             };         }          private regex createregex(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 + "$");         }          private routevaluedictionary removedomaintokens(routevaluedictionary values)         {             regex tokenregex = new regex(@"({[a-za-z0-9_]*})*-?\.?\/?({[a-za-z0-9_]*})*-?\.?\/?({[a-za-z0-9_]*})*-?\.?\/?({[a-za-z0-9_]*})*-?\.?\/?({[a-za-z0-9_]*})*-?\.?\/?({[a-za-z0-9_]*})*-?\.?\/?({[a-za-z0-9_]*})*-?\.?\/?({[a-za-z0-9_]*})*-?\.?\/?({[a-za-z0-9_]*})*-?\.?\/?({[a-za-z0-9_]*})*-?\.?\/?({[a-za-z0-9_]*})*-?\.?\/?({[a-za-z0-9_]*})*-?\.?\/?");             match tokenmatch = tokenregex.match(domain);             (int = 0; < tokenmatch.groups.count; i++)             {                 group group = tokenmatch.groups[i];                 if (group.success)                 {                     string key = group.value.replace("{", "").replace("}", "");                     if (values.containskey(key))                         values.remove(key);                 }             }              return values;         }     } } 

domaindata.cs

using system; using system.collections.generic; using system.linq; using system.web;  namespace mvcdomainrouting.code {     public class domaindata     {         public string protocol { get; set; }         public string hostname { get; set; }         public string fragment { get; set; }     } } 

linkextensions.cs

using system; using system.collections.generic; using system.linq; using system.web; using system.web.routing; using system.web.mvc; using mvcdomainrouting.code;  namespace system.web.mvc.html {     public static class linkextensions     {         public static string actionlink(this htmlhelper htmlhelper, string linktext, string actionname, bool requireabsoluteurl)         {             return htmlhelper.actionlink(linktext, actionname, null, new routevaluedictionary(), new routevaluedictionary(), requireabsoluteurl);         }          public static string actionlink(this htmlhelper htmlhelper, string linktext, string actionname, object routevalues, bool requireabsoluteurl)         {             return htmlhelper.actionlink(linktext, actionname, null, new routevaluedictionary(routevalues), new routevaluedictionary(), requireabsoluteurl);         }          public static string actionlink(this htmlhelper htmlhelper, string linktext, string actionname, string controllername, bool requireabsoluteurl)         {             return htmlhelper.actionlink(linktext, actionname, controllername, new routevaluedictionary(), new routevaluedictionary(), requireabsoluteurl);         }          public static string actionlink(this htmlhelper htmlhelper, string linktext, string actionname, routevaluedictionary routevalues, bool requireabsoluteurl)         {             return htmlhelper.actionlink(linktext, actionname, null, routevalues, new routevaluedictionary(), requireabsoluteurl);         }          public static string actionlink(this htmlhelper htmlhelper, string linktext, string actionname, object routevalues, object htmlattributes, bool requireabsoluteurl)         {             return htmlhelper.actionlink(linktext, actionname, null, new routevaluedictionary(routevalues), new routevaluedictionary(htmlattributes), requireabsoluteurl);         }          public static string actionlink(this htmlhelper htmlhelper, string linktext, string actionname, routevaluedictionary routevalues, idictionary<string, object> htmlattributes, bool requireabsoluteurl)         {             return htmlhelper.actionlink(linktext, actionname, null, routevalues, htmlattributes, requireabsoluteurl);         }          public static string actionlink(this htmlhelper htmlhelper, string linktext, string actionname, string controllername, object routevalues, object htmlattributes, bool requireabsoluteurl)         {             return htmlhelper.actionlink(linktext, actionname, controllername, new routevaluedictionary(routevalues), new routevaluedictionary(htmlattributes), requireabsoluteurl);         }          public static string actionlink(this htmlhelper htmlhelper, string linktext, string actionname, string controllername, routevaluedictionary routevalues, idictionary<string, object> htmlattributes, bool requireabsoluteurl)         {             if (requireabsoluteurl)             {                 httpcontextbase currentcontext = new httpcontextwrapper(httpcontext.current);                 routedata routedata = routetable.routes.getroutedata(currentcontext);                  routedata.values["controller"] = controllername;                 routedata.values["action"] = actionname;                  domainroute domainroute = routedata.route domainroute;                 if (domainroute != null)                 {                     domaindata domaindata = domainroute.getdomaindata(new requestcontext(currentcontext, routedata), routedata.values);                     return htmlhelper.actionlink(linktext, actionname, controllername, domaindata.protocol, domaindata.hostname, domaindata.fragment, routedata.values, null);                 }             }             return htmlhelper.actionlink(linktext, actionname, controllername, routevalues, htmlattributes);         }     } } 

edit global.asax (or in more recent versions of mvc, these go appstart\routeconfig.cs)

using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using system.web.routing; using mvcdomainrouting.code;  namespace mvcdomainrouting {     // note: instructions on enabling iis6 or iis7 classic mode,      // visit http://go.microsoft.com/?linkid=9394801      public class mvcapplication : system.web.httpapplication     {         public static void registerroutes(routecollection routes)         {             routes.ignoreroute("{resource}.axd/{*pathinfo}");              routes.add("domainroute", new domainroute(                 "home.example.com",                                     // domain parameters                 "{action}/{id}",                                        // url parameters                 new { controller = "home", action = "index", id = "" }  // parameter defaults             ));              //routes.add("domainroute", new domainroute(             //    "{controller}.example.com",                             // domain parameters             //    "{action}/{id}",                                        // url parameters             //    new { controller = "home", action = "index", id = "" }  // parameter defaults             //));              //routes.add("domainroute", new domainroute(             //    "{controller}-{action}.example.com",                             // domain parameters             //    "{id}",                                        // url parameters             //    new { controller = "home", action = "index", id = "" }  // parameter defaults             //));              routes.maproute(                 "default",                                              // route name                 "{controller}/{action}/{id}",                           // url parameters                 new { controller = "home", action = "index", id = "" }  // parameter defaults             );          }          protected void application_start()         {             registerroutes(routetable.routes);         }     } } 

also see gist solution (and discussion) here.


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -