【问题标题】:.net routing: Possible to restrict route to a certain domain?.net 路由:可以限制到某个域的路由吗?
【发布时间】:2014-05-03 19:17:42
【问题描述】:

我继承了一个处理几个域的 vb.net WebForms 项目。但是,我想要每个域的唯一路由。当我使用 MapPageRoute 时,有没有办法通过获取域来做到这一点?还是我需要做类似的事情:

routes.MapPageRoute("r1", "example1/page1", "~/example1/default.aspx")
routes.MapPageRoute("r2", "example2/page1", "~/example2/default.aspx")

但是 URL 需要像这样:

//example1.com/example1/page1 和 //example2.com/example2/page1

如果可能,我想在 Application_Start 中将路由限制到特定域。

* 编辑 *

好的,看来我可以通过为相似的路径创建唯一的路径名称来半解决这个问题:

routes.MapPageRoute("r1", "page1", "~/example1/default.aspx")
routes.MapPageRoute("r2", "page1", "~/example2/default.aspx")

然后在我的标记中我可以做到:

<asp:HyperLink NavigateUrl="<%$RouteUrl:routename=r1%>" ID="link_home" runat="server">Home</asp:HyperLink>

然后在我的默认页面(或其母版页)中,我可以通过重定向到基于域的相应路由来处理“//example.com/”请求。

但是我不确定如何处理传入的请求,例如:

//example1.com/page1 和 //example2.com/page1。我假设第一条路线将为任一域加载。有什么想法我能做什么?

【问题讨论】:

标签: asp.net vb.net routing routes


【解决方案1】:

跟进我的评论:

您可以改为基于域创建约束。您需要继承 IRouteConstraint 接口。

您定义路线的位置:

Dim domain1Constraint As New HostConstraint("domain1.com")
routes.MapPageRoute("r1", "page1", "~/example1/default.aspx", False, Nothing, New RouteValueDictionary(New With {domain1Constraint }))

然后创建一个类HostConstraint

Imports System
Imports System.Web.Routing

Public Class HostConstraint
    Implements IRouteConstraint

    Private _host As String
    Public Sub New(ByVal host As String)
        _host = host.ToLower()
    End Sub

    Public Function Match(ByVal httpContext As HttpContextBase, _
                          ByVal route As Route, _
                          ByVal parameterName As String, _
                          ByVal values As RouteValueDictionary, _
                          ByVal routeDirection As RouteDirection) As Boolean Implements IRouteConstraint.Match

        Dim host As String = httpContext.Request.Url.Host.ToLower()

        If host.Contains(_host) Then
            Return True
        Else
            Return False
        End If
    End Function
End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 2011-07-18
    相关资源
    最近更新 更多