URL Writing in .NET 3.5 SP1

I have just finished one of my first complete ASP.NET projects at my workplace. I learned a ton while going through the process, but one item stands out: URL Routing. While developing the MVC framework, Microsoft added routing capabilities into SP1 of the 3.5 .NET framework. The great thing is that the features can be used outside of the MVC framework, so you can use these in any ASP.NET application. What does this mean? In the most basic terms it allows you to rewrite your URL’s into very friendly looking and SEO safe strings. This is done through your code, so there is no monkeying around with the server or IIS. Below is a quick example of the most basic route you could use. It just maps a URL that contains one variable to the handling page. All kinds of other stuff can be set up that are not covered here (multiple variables, default values, etc…)

In the global.asax file add the following function:

<code lang="csharp[lines]">private static void RegisterRoutes(RouteCollection routes)<br></br>  
    {<br></br>
        routes.Add(new Route("product/{name}", new ProductRouteHandler()));<br></br>
    }```

This code tells the route handler to look for a url in the form of /product and look in a class called ProductRouteHandler to see how we want to process it. The {name} is our variable name, so in our handler class we can call this variable by this name.

Below is the code for our ProductRouteHandler class:

using System;


using System.Web.Routing;


using System.Web;


using System.Web.UI;


using System.Web.Compilation;```

///

/// Route Handler for products
///
public class ProductRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string virtualPath = string.Format("~/products/detail.aspx");
foreach (var value in requestContext.RouteData.Values)
{
requestContext.HttpContext.Items[value.Key] = value.Value;
}
return (Page)BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page));

}

}

This code is fairly simple, with the most important piece being the virtual path variable. This variable says that when a request comes in matching this route it will be handled by a file named detail.aspx in our products directory. We now have our route set up and ready to use.

The last piece of information you need to know is how to use the variable that the route set up for us. In our global.asax file we specified a variable named “name”. So in our detail.aspx file, all you need to do to use this variable is grab it out of the request context like so:

<code lang="csharp[lines]">Context.Items["name"]

So you can then pull your product information from your database using this variable.

That’s it! You now have SEO friendly URL’s for your product pages. So what was the result here?

http://www.yourdomain.com/product/TheBestProduct is handled by the code living in /products/detail.aspx

Again, this is the most simple form of a route and does not touch on some of the other cool things you can do with this feature.

Comments

comments powered by Disqus