ASP.NET 4.0 Feature - URL Routing

Let’s see the new feature in ASP.NET 4.0 known as “URL Routing”. URL Routing is a feature which is driven by the ASP.NET URL routing engine which enables the application to route to custom urls instead of actual urls which may have some querystrings etc. In the past we have seen urls like http://localhost/NewStore/Titles.aspx can be now show as http://localhost/NewStore/AllTitles or http://localhost/NewStore/Titles.aspx?ID=2 can be now shown as http://localhost/NewStore/Products/Fiction . Doesn’t this look great? Now let’s see how do we achieve this.

We would be using the “System.Web.Routing” class which products us a method “MapPageRoute” which helps to do the routing. Let us see a simple example where we would have two pages one which shows All the Titles and another which shows Title for a specific Author Id. For this example I am using the Pubs Database.

We would have to first create the two new Pages “Titles.aspx” and “TitlesbyAuthor.aspx” . Now the “Titles.aspx” shows All the Titles and “TitlebyAuthor.aspx” shows the Titles based on the Author ID. We have a create a main page “Default.aspx” which would link to the respective pages, the source code is as seen below



Now if you notice how the urls in the href tags are, these are nothing but the new routing urls, Now let’s see the changes in the “Global.asax” which would facilitate the use of the “MapPageRoute” method to enable the above urls to work as seen below.



If you see the code the first one is routing to the “Titles.aspx”. The second one is routing to the “TitlesbyAuthor.aspx” based on the Author ID.
We can write our necessary code in the both pages “Page_Load” to bind the data to the gridview.

Hope this was cool feature. Please add your comments or suggestions.

Cheers!!!

Comments

Popular posts from this blog

Jesus - God Thinks of you.

Tips on JQuery Intellisense in VS 2008