views:

199

answers:

2

What's the best way to adjust the path destination for a routing table created in the global.asax Application_Start event based on the domain/sub domain/host? The following worked in IIS6, but with IIS7 the request object is decoupled from the Application_Start event and therefore does not work anymore:

Dim strHost As String = Context.Request.Url.Host  
Dim strDir As String = ""  
If strHost.Contains("domain1.com") Then  
    strDir = "area1/"  
Else  
    strDir = "area2/"  
End If  
routes.MapPageRoute("Search", "Search", "~/" & strDir & "search.aspx") 
A: 

Is this a setting you could read from web.config instead? <- my recommendation.

Does this post help?

http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx

Raj Kaimal
My example is siplified from the actual implementation, and the logic involved won't work out by using web.config (defining static routes).Yeah, I've seen that article. I don't believe moving page route mapping to begin_request will work, and the AppDomainAppVirtualPath doesn't help since I have logic based on the host name.My guess is the solution has something to do with overloading the means by which the routing is fired off at the page request level, and not where the routes are defined (at application_start). I just don't have a clue as to how to do it. Any ideas or suggestions?
DrewF
I was talking about storing "domain1.com" in your web.config under appsettings.
Raj Kaimal
+1  A: 

I seem to have solved my own problem. You can't access the Request object at Application_Start with IIS7.0, though you can use it in a custom route constraint. Here's how I did it.

Define the custom route constraint:

Imports System.Web
Imports System.Web.Routing

Public Class ConstraintHost
    Implements IRouteConstraint

    Private _value As String

    Sub New(ByVal value As String)
        _value = value
    End Sub

    Public Function Match(ByVal httpContext As System.Web.HttpContextBase, ByVal route As System.Web.Routing.Route, ByVal parameterName As String, ByVal values As System.Web.Routing.RouteValueDictionary, ByVal routeDirection As System.Web.Routing.RouteDirection) As Boolean Implements System.Web.Routing.IRouteConstraint.Match
        Dim hostURL = httpContext.Request.Url.Host.ToString()
        Return hostURL.IndexOf(_value, StringComparison.OrdinalIgnoreCase) >= 0
    End Function
End Class

Then define the route:

routes.MapPageRoute(
    "Search_Area1",
    "Search",
    "~/area1/search.aspx",
    True,
    Nothing,
    New RouteValueDictionary(New With {.ArbitraryParamName = New ConstraintHost("domain1.com")})
)

routes.MapPageRoute(
    "Search_Area2",
    "Search",
    "~/area2/search.aspx")
)

This technique can also be used for applying different routing based on the sub-domain as well.

Big thanks to Steven Wather's asp.net mvc routing post for pointing me in the right direction (even though it was for mvc and not web forms).

DrewF