如何根据域/主机调整asp.net URL路由?

发布于 2024-08-31 15:31:27 字数 425 浏览 6 评论 0原文

根据域/子域/主机调整在 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") 

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") 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦太阳 2024-09-07 15:31:27

我似乎已经解决了我自己的问题。您无法使用 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 路由 帖子为我指明了正确的方向(即使它是针对 mvc 而不是 Web 表单)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文