ASP.NET Routing

Add a handler to the web.config

<add assembly="System.Web.Routing, Version=3.5.0.0,   Culture=neutral,   PublicKeyToken=31BF3856AD364E35"/>

Create a custom route handler class

Imports System.Web.Compilation
Imports System.Web.Routing
 
Public Class CustomRouteHandler
    Implements IRouteHandler
 
    Private _virtualPath As String
 
    Public Sub New(ByVal vPath As String)
        _virtualPath = vPath
    End Sub
 
    Public Property VirtualPath() As String
        Get
            Return _virtualPath
        End Get
        Private Set(ByVal value As String)
            _virtualPath = value
        End Set
    End Property
 
    Public Function GetHttpHandler(ByVal requestContext As System.Web.Routing.RequestContext) As System.Web.IHttpHandler Implements System.Web.Routing.IRouteHandler.GetHttpHandler
        Dim redirectPage As IHttpHandler
        redirectPage = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, GetType(Page))
        Return redirectPage
    End Function
 
End Class

Add this to the Global.asax

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RegisterRoutes(RouteTable.Routes)
End Sub
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    Dim customerUrlPattern As String
    Dim customerRoute As Route
 
    customerUrlPattern = "customer/{customernumber}"
    customerRoute = New Route(customerUrlPattern, New CustomRouteHandler("~/CustomerEdit.aspx"))
  
    routes.Add("customerRoute", customerRoute)
End Sub

in the target page (in our example above 'customeredit.aspx') retrieve the customer number

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
        Dim strCustomerNumber As String = RouteData.Values.Values(0)
        lblCustomerNumber.Text = strCustomerNumber
    Catch ex As Exception
 
    End Try
End Sub

 

 

 

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences