Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 1st, 2010, 02:42 PM
Registered User
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default url routing in asp.net 3.5 web form


I've been going through all the blogs and articles that I could find to get a grip on url routing hoping to find an easy-to-follow example of this technique. I haven't found much, at least from a non-expert developer's point of view, such as myself.

I've managed somehow to grasp 4 steps: 1) add the reference to the System.Web.Routing namespace, 2) register the routehandlers in the global.asax file, 3) add the routing module in the httpModule section of the web.config file, 4) write the routehandler class which implements the IRouteHandler interface and its GetHttpHandler function.

What I'd like to know is how I can get at the values of the url parameters and how I can provide dynamic data as values as I normally would using query string parameters?

Thank you so much for any answer.
Old April 10th, 2010, 02:06 PM
Registered User
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile One possible solution

After disassembling and trying to understand Scott Mitchell's example of url routing in asp.net 3.5 with web forms, I've come up with a very simple sample in vb.net, which is my favourite language. Basically, in the class file containing the routehandler which has been registered in Global.asax, the parameter is first grabbed from the route data values of the RequestContext:
   Dim categoryName As String =   requestContext.RouteData.Values("CategoryName")
Then it's added to the items in the current httpcontext:

 Dim catName As String = categoryName
 System.Web.HttpContext.Current.Items("Category") = catName
                Return CType(BuildManager.CreateInstanceFromVirtualPath("~/CategoryProducts.aspx", GetType(Page)), Page)
In the page that needs to grab the parameter from the url in order to display the right item, just grab the value of the item in the items collection of the current httpContext object, normally you'd have used a Request.QueryString to retrieve this value:

 Dim category As String = HttpContext.Current.Items("Category")
The Url is dynamically reconstructed by using a Helpers class with a method such as this:

 Public Shared Function FormatCategoryUrl(ByVal categoryName As String) As String
        Dim strVirtualPathData As VirtualPathData
        Dim params As RouteValueDictionary

        params = New RouteValueDictionary(New With {.categoryName = categoryName})

        strVirtualPathData = RouteTable.Routes.GetVirtualPath(Nothing, "View Category", params)
        Return strVirtualPathData.VirtualPath

    End Function
For example, in the hyperlink that leads to the products by category page, its NavigateUrl property will be something like:

 <asp:HyperLink ID="hlGoToCategory" runat="server" 
            NavigateUrl='<%# Helpers.FormatCategoryUrl(Eval("CategoryName").ToString()) %>' 
            Text='<%#Eval("CategoryName") %>'></asp:HyperLink>
I understand that things might get a bit more complicated by adding authentication and authorization for example, but I just needed an extremely simple application so that I could grasp the basic workings of the url routing mechanism. Also, I'm aware that the all thing has been simplified in .Net 4.

Similar Threads
Thread Thread Starter Forum Replies Last Post
URL Routing elwappo ASP.NET 3.5 Professionals 2 June 11th, 2009 01:20 AM
URL Routing elwappo ASP.NET 2.0 Professional 4 May 26th, 2009 04:27 PM
Problem with Posting an ASP .Net web form eliea ASP.NET 1.0 and 1.1 Basics 4 September 11th, 2006 02:59 AM
Detect Web Form Field Changes in ASP.NET. sand133 ASP.NET 2.0 Professional 1 August 22nd, 2006 10:15 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.