根据域/子域/主机调整在 global.asax Application_Start 事件中创建的路由表的路径目标的最佳方法是什么?以下内容在 IIS6 中有效,但在 IIS7 中,请求对象与 Application_Start 事件分离,因此不再有效:
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")
最佳答案
我似乎已经解决了我自己的问题。您无法使用 IIS7.0 访问 Application_Start 处的 Request 对象,但您可以在自定义路由约束中使用它。我就是这样做的。
定义自定义路由约束:
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
然后定义路线:
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")
)
此技术也可用于基于子域应用不同的路由。
非常感谢 Steven Wather 的 asp.net mvc routing为我指明正确方向的帖子(即使它是针对 MVC 而不是 Web 表单)。
关于asp.net - 如何根据域/主机调整asp.net URL路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2796035/