I stumbled across Scorpion's answer on this thread and wanted to give it a shot with URL Routing in ASP.NET 4.0. So I came up with writing a new version of Scorpion's httpModule and thought I'd share it with you guys:
Imports System.Web
Public Class SubdomainHttpModule
Implements IHttpModule
Public Property subdomain() As String
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
Public Sub Init(app As HttpApplication) Implements IHttpModule.Init
AddHandler app.BeginRequest, AddressOf MyBeginRequest
AddHandler app.EndRequest, AddressOf MyEndRequest
End Sub
Public Sub MyBeginRequest(ByVal s As Object, ByVal e As EventArgs)
Dim app As HttpApplication
app = CType(s, HttpApplication)
Dim url As String = HttpContext.Current.Request.Headers("HOST")
Dim index As Integer = url.IndexOf(".")
Dim subdomain As String
If index > 0 Then
subdomain = url.Substring(0, index)
Else
subdomain = "www"
End If
If Me.subdomain = "" Then
Me.subdomain = subdomain
End If
If Me.subdomain <> subdomain Then
HttpRuntime.UnloadAppDomain()
app.Response.Redirect(app.Request.Path)
End If
End Sub
Public Sub MyEndRequest(ByVal s As Object, ByVal e As EventArgs)
End Sub
End Class
The above, with the combination of the code below (in global.asax), works like a charm when routing:
Private Function GetSubdomain() As String
Dim url As String = HttpContext.Current.Request.Headers("HOST")
Dim index As Integer = url.IndexOf(".")
If index > 0 Then
Return url.Substring(0, index)
Else
Return "www"
End If
End Function
Sub RegisterRoutes(ByVal routes As RouteCollection)
If GetSubdomain() = "www" Then
RouteTable.Routes.MapPageRoute("default", String.Empty, "~/site/default.aspx")
Else
RouteTable.Routes.MapPageRoute("default", String.Empty, "~/site/subdomain/default.aspx")
End If
End Sub
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
RegisterRoutes(RouteTable.Routes)
End Sub
Each time the visitor switches from one subdomain to another, or browsers to the root of the domain, the application is restarted and response redirected to the original request. Doing so, re-registeres the routes as needed.
Enjoy!
P.S. To use the httpModule in your web app, you will also have to reference it in your web.cofig:
nickjo
Member
2 Points
1 Post
Re: Creating a subdomain
Jun 02, 2009 09:36 AM|LINK
hii scorpion4000,
Thanks for your valuable support , i was trying to use your code in c# after converting it to c#, but it doesnt work . can u send me the code in c#.
Thanks for help !!
zishandanish
Member
159 Points
88 Posts
Re: Creating a subdomain
Feb 16, 2012 01:47 AM|LINK
Hello everyone,
I stumbled across Scorpion's answer on this thread and wanted to give it a shot with URL Routing in ASP.NET 4.0. So I came up with writing a new version of Scorpion's httpModule and thought I'd share it with you guys:
Imports System.Web Public Class SubdomainHttpModule Implements IHttpModule Public Property subdomain() As String Public Sub Dispose() Implements IHttpModule.Dispose End Sub Public Sub Init(app As HttpApplication) Implements IHttpModule.Init AddHandler app.BeginRequest, AddressOf MyBeginRequest AddHandler app.EndRequest, AddressOf MyEndRequest End Sub Public Sub MyBeginRequest(ByVal s As Object, ByVal e As EventArgs) Dim app As HttpApplication app = CType(s, HttpApplication) Dim url As String = HttpContext.Current.Request.Headers("HOST") Dim index As Integer = url.IndexOf(".") Dim subdomain As String If index > 0 Then subdomain = url.Substring(0, index) Else subdomain = "www" End If If Me.subdomain = "" Then Me.subdomain = subdomain End If If Me.subdomain <> subdomain Then HttpRuntime.UnloadAppDomain() app.Response.Redirect(app.Request.Path) End If End Sub Public Sub MyEndRequest(ByVal s As Object, ByVal e As EventArgs) End Sub End ClassThe above, with the combination of the code below (in global.asax), works like a charm when routing:
Private Function GetSubdomain() As String Dim url As String = HttpContext.Current.Request.Headers("HOST") Dim index As Integer = url.IndexOf(".") If index > 0 Then Return url.Substring(0, index) Else Return "www" End If End Function Sub RegisterRoutes(ByVal routes As RouteCollection) If GetSubdomain() = "www" Then RouteTable.Routes.MapPageRoute("default", String.Empty, "~/site/default.aspx") Else RouteTable.Routes.MapPageRoute("default", String.Empty, "~/site/subdomain/default.aspx") End If End Sub Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) RegisterRoutes(RouteTable.Routes) End SubEach time the visitor switches from one subdomain to another, or browsers to the root of the domain, the application is restarted and response redirected to the original request. Doing so, re-registeres the routes as needed.
Enjoy!
P.S. To use the httpModule in your web app, you will also have to reference it in your web.cofig:
<system.web> <httpModules> <add name="SubdomainHttpModule" type="SubdomainHttpModule" /> </httpModules> </system.web>jayminsoneji
Member
2 Points
1 Post
Re: Creating a subdomain
Feb 12, 2013 03:57 PM|LINK
Hello friend,
can u please provide me demo code for your solution. i want it desperately
thanks in advance,
Jaymin Soneji