URL Routing with ASP.Net 4.0

URL Routing in ASP.Net 4.0 allows us to easily set up “friendly” URL’s.

So instead of having URLs like mysite.com/ContactUs.aspx, mysite.com/ContentPage.aspx?ID=12, etc, you can instead have mysite.com/contact, mysite.com/our-products.

Setting up URL Routing is relatively straight forward.

First, open Global.asax.cs file for editing.

Add using System.Web.Routing; in header.

Create a new method (your short names and physical file names may be different):

void RegisterRoutes(RouteCollection routes)
        {
            routes.MapPageRoute("home", "home", "~/Default.aspx");
            routes.MapPageRoute("contact", "contact", "~/Contact.aspx");
            routes.MapPageRoute("solutions", "solutions", "~/Solutions.aspx");
            routes.MapPageRoute("who-we-are", "who-we-are", "~/WhoWeAre.aspx");
        }

Now call this method from within Application_Start event:

void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RegisterRoutes(RouteTable.Routes);
        }

Rebuild your project, and new URL routes are ready to be used.

Overall a very simple and effective method. This article just scratches the beginning, you can read more about URL Routing in this MSDN article.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s