You can do it by creating a new route and adding it to the routes collection in RegisterRoutes in your global.asax. Below is a very simple example of a custom Route:
public class ExampleRoute : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var url = httpContext.Request.Headers["HOST"];
var index = url.IndexOf(".");
if (index < 0)
return null;
var subDomain = url.Substring(0, index);
if (subDomain == "user1")
{
var routeData = new RouteData(this, new MvcRouteHandler());
routeData.Values.Add("controller", "User1"); //Goes to the User1Controller class
routeData.Values.Add("action", "Index"); //Goes to the Index action on the User1Controller
return routeData;
}
if (subDomain == "user2")
{
var routeData = new RouteData(this, new MvcRouteHandler());
routeData.Values.Add("controller", "User2"); //Goes to the User2Controller class
routeData.Values.Add("action", "Index"); //Goes to the Index action on the User2Controller
return routeData;
}
return null;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
//Implement your formating Url formating here
return null;
}
}
Hope this helpful
Regards
Young Yang
Routingasp.netmvc
Please mark the replies as answers if they help or unmark if not.
Feedback to us
Sunil Prakas...
Member
231 Points
46 Posts
MVC 3 Routing like Sub-Domain
Apr 12, 2012 05:26 AM|LINK
Hello Experts,
I would like to know it's possible to mvc 3 routing like sub-domain for example (abc.com/blog to blog.abc.com it should be dymanic.)
Thanks in advance.
Routing asp.netmvc
Sunil Prakash Paudel
Software Engineer @
KRB Software
Please "Mark As Answered" if it helped you.
ignatandrei
All-Star
134989 Points
21641 Posts
Moderator
MVP
Re: MVC 3 Routing like Sub-Domain
Apr 12, 2012 07:50 AM|LINK
just read
http://www.dotnetexpertguide.com/2012/04/aspnet-iis-dns-records-sub-domain-on.html
Routing asp.netmvc
Young Yang -...
All-Star
21343 Points
1818 Posts
Microsoft
Re: MVC 3 Routing like Sub-Domain
Apr 19, 2012 02:39 AM|LINK
Hi
You can do it by creating a new route and adding it to the routes collection in RegisterRoutes in your global.asax. Below is a very simple example of a custom Route:
public class ExampleRoute : RouteBase { public override RouteData GetRouteData(HttpContextBase httpContext) { var url = httpContext.Request.Headers["HOST"]; var index = url.IndexOf("."); if (index < 0) return null; var subDomain = url.Substring(0, index); if (subDomain == "user1") { var routeData = new RouteData(this, new MvcRouteHandler()); routeData.Values.Add("controller", "User1"); //Goes to the User1Controller class routeData.Values.Add("action", "Index"); //Goes to the Index action on the User1Controller return routeData; } if (subDomain == "user2") { var routeData = new RouteData(this, new MvcRouteHandler()); routeData.Values.Add("controller", "User2"); //Goes to the User2Controller class routeData.Values.Add("action", "Index"); //Goes to the Index action on the User2Controller return routeData; } return null; } public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { //Implement your formating Url formating here return null; } }Hope this helpful
Regards
Young Yang
Routing asp.netmvc
Feedback to us
Develop and promote your apps in Windows Store