Asp.Net 4.0 URL Routing

It’s been  really easy  in Asp.Net 4.0 for building SEO friendly URL applications with VS 2010. In one my previous article I have explained how we can achieve the same in Asp.Net 3.5  (check it out).  In Asp.Net 3.5 we need to implement the IRouteHandler interface in custom route handler class and bla bla….but now in Asp.Net 4.0 we do not need all extra code.  I am following the same example in my previous article here.  In the Global.asax file I am registering the routes some thing like below

You can download the entire article from here

Global.asax file Configuration

Add Import directive to Import System.Web.Routing name space

<%@ Import Namespace=”System.Web.Routing” %>

void RegisterRoutes(RouteCollection routes)

 void Application_Start(object sender, EventArgs e)

The first parameter member/{name} which denote the url structure, here the url which starts /member/{name} (ex : /member/deepu/) will point physically to the following path ~/member/member-list.aspx

Create a aspx page called default.aspx

This page will have a list of Members in Tabular format. I am going to use List view control to achieve this.

<asp:ListView ID=”lvUserList” runat=”server”>
<table width=”50%” style=”background-color: lightgrey;” cellpadding=”2″ cellspacing=”1″
<tr style=”background-color: white”>
<td width=”100″ align=”center”>
Member ID
<td align=”center”>
<td width=”75″ align=”center”>
<td width=”50″>
<asp:PlaceHolder ID=”itemPlaceholder” runat=”server” />
<tr style=”background-color: white”>
<td align=”center”>
<asp:Label ID=”lab” Text='<%# DataBinder.Eval(Container.DataItem, “MemberID”)%>’
runat=”server” />
<asp:Label ID=”Label1″ Text='<%# DataBinder.Eval(Container.DataItem, “Name”)%>’ runat=”server” />
<td align=”center”>
<a target=”_blank” href='<%# DataBinder.Eval(Container.DataItem, “BlogURL”)%>’>Blog</a>
<td align=”center”>
<a href=”<%= ResolveUrl(“~/member/”)%><%# DataBinder.Eval(Container.DataItem, “Name”)%>”>
<br />

Default.aspx.cs code behind

public partial class Memeber
 public int MemberID { get; set; }
 public string Name { get; set; }
 public string BlogURL { get; set; }

public partial class _Default : System.Web.UI.Page
 protected void Page_Load(object sender, EventArgs e)
 if (!IsPostBack)

 void BindUsers()
 var items = new List<Memeber>()
 new Memeber {MemberID = 1,Name = "Deepu", BlogURL = ""},
 new Memeber {MemberID = 2,Name = "Scott", BlogURL = ""},
 new Memeber {MemberID = 3,Name = "Joe", BlogURL = ""},

 lvUserList.DataSource = items.ToList();

We have completed the listing page. The next page will be the detail page which will be rendering the member name for the URL routing.

Member-List.aspx Page

<asp:Content ID=”BodyContent” runat=”server” ContentPlaceHolderID=”MainContent”>
Member Name : <asp:Label ID=”labMemberName” runat=”server” />

Member-List.aspx code behind Page

var name = Page.RouteData.Values["name"];
 if (name != null)
 Page.Title = name.ToString();
 labMemberName.Text = name.ToString();

You can download the entire article from here

Hope this help and If you have any comments, please feel free to write your feedback.


3 thoughts on “Asp.Net 4.0 URL Routing

  1. Possibly one of the rarest article in the Internet. By the way.. I got an error cannot use a leading .. to exit above the top directory. and get rid of replacing ../ with ~/ for images in the page.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s