2014-03-25 101 views
1

我繼承了一個處理兩個域的vb.net WebForms項目。但是,我需要每個域的唯一路由。有沒有辦法通過在使用MapPageRoute時獲取域來實現這一點?或將我需要做的是這樣的:.net路由:可能限制路由到某個域?

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

但隨後的網址需要像:

//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> 
在我的缺省頁(或它的主頁),然後我可以處理通過重定向到相應的R中的「//example.com/」請求

然後oute基於域。

然而我不知道如何處理傳入的請求,如:

//example1.com/page1和//example2.com/page1。我假設第一條路線將加載任一域。任何想法我可以做什麼?

+0

您可能需要重寫GetRouteData功能ALA:http://stackoverflow.com/questions/278668/is-it-make-an-asp-net-mvc-route-based-on-a-subdomain – Chris

回答

2

跟進到我的評論:

可以代替創建基於域的約束。您需要繼承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 
+2

謝謝!這工作完美。 – jrjensen