Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Jun 7

Written by: Steve Gray
6/7/2010 8:12 AM  RssIcon

Those of you that have followed this site for a while know that I’m not long on exactly how (or why) a technology works, what goes here are code samples. Stuff that I can look up and have a working example in a few minutes.

Today’s topic is URL routing in ASP.NET, using the .NET 4.0 Framework.

What this does for you is allow you to take a standard URL, say http://mysite.com/products?productID=toast (or worse, http://mysite.com/products?prodid=123) and change it to http://mysite.com/products/toast. The end result is much more ‘Google friendly’, and user friendly, and … cool.

Here we go.

My code and screenshots are all from Visual Studio 2010, which I’m really enjoying. First, add Global.aspx to your site if it’s not already there. Add this code:

<%@ Application Language="VB" %>
<%@ Import Namespace="System.Web.Routing" %>
<script runat="server">
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        RegisterRoutes(RouteTable.Routes)
    End Sub
    Sub registerRoutes(ByVal routes As routecollection)
        routes.MapPageRoute("test route", "Products/{*ProductName}", "~/Products.aspx")
    End Sub

Note the ‘Import’ directive at the top, you’ll need that.

This adds a ‘route’ to the site. “test route” is the name of the route, “Products/{*ProductName}” is the pattern that the site will look for. If the site sees a request for “Products/<anything>”, it will take that request and pass it to the page that we designate in the third parameter (“~/Products.aspx”) and (here’s the magic) it will pass everything after “Products/” to that page in the ‘routes.routedata’ collection. Follow along, you’ll see.

In default.aspx, add a link to the products page:

 View product <asp:HyperLink NavigateUrl="~/Products/asp.net" ID="lnkProduct" 
runat="server">Products/asp.net</asp:HyperLink>

Create a page called ‘Products.aspx’. Add a label:

<asp:Label id="lblProduct" runat="server"></asp:Label>

 

Finally, add code behind that page:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strProduct As String = Page.RouteData.Values("ProductName")
        Me.lblProduct.Text = "Product: " & strProduct
    End Sub

That’s it.

In the Page Load, we get the product name from the Page.RouteData collection, and display it on the page in a label.

Note that when we navigate to the products.aspx page the URL is changed to “Products/asp.net” and the label on the page displays our ‘product’, asp.net.

 

image


There are good tutorials on the subject here:

4GuysFromRolla

Dan Maharry

ScottGu

Tags:
Categories:
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us