1

I inherited a vb.net WebForms project that handles a couple domains. However, I want unique routes for each domain. Is there a way to do this by getting the domain when I use MapPageRoute? Or will I need to do something like:

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

But then the urls will need to be like:

//example1.com/example1/page1 and //example2.com/example2/page1

At Application_Start, I'd like to constrain a route to a specific domain, if possible.

* EDIT *

Ok, it looks like I was able to semi-resolve this by creating unique route names for similar route paths:

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

Then in my markup I can do:

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

Then in my Default page (or its master page), I can then handle the "//example.com/" request by redirecting to the respective route based on the domain.

However I'm not sure how to handle incoming requests like:

//example1.com/page1 and //example2.com/page1. I assume the first route will load for either domain. Any ideas what I can do?

jrjensen
  • 191
  • 9
  • You may need to Override the GetRouteData function ala: http://stackoverflow.com/questions/278668/is-it-possible-to-make-an-asp-net-mvc-route-based-on-a-subdomain – Chris Mar 26 '14 at 15:57

1 Answers1

2

Follow up to my comment:

You can instead create a constraint based on the domain. You'll need to subclass the IRouteConstraint interface.

Where you define your routes:

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

Then create a class 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
Chris
  • 1,731
  • 4
  • 18
  • 28