Saturday, February 19, 2011

Create a Link (URL) class for ASP.NET and C#

I was faced with a requirement last week to build a dynamic link generator to manage URL's in a site. The project owner did not want to build the URL in the presentation layer as the site has an ecommerce architecture where there are product categories and products department, which include a category in itself.

Of course, if you think about it, every time that the user clicks on a category or department, the site needs to create a dynamic URL like Catalog.aspx?DepartmentID=1". The Query string will be used to retrieve the value of DepartmentId, which in our example is equal to 1.

At the beginning I thought about creating a struct that could contain all the different elements that the site offers in its menus, such as Departments, Categories, Products, ect. The problem was at some point I would have to be always assigning a new value to these elements from the presentation layer, and that could become counterproductive in the long run.

So I came up with a class called Link. This class has several methods, but the most important one is the one called BuildAbsolute (See the code below). This class builds an absolute URL, by building an absolute path by using the HTTPContext and HTTPUtility classes for more info on these classes refer to the following links:

So, the presentation layer will call this class with the parameters from the presentation control objects, such a the department ID, CategoryID, Page, etc.

using System;using System.Collections.Generic;using
<summary> Summary description for Link/// </summary>public class Link{
public Link()//// TODO: Add constructor logic here//}
//Builds an absolute URL
private static string BuildAbsolute(string relativeUri)}//Generate a department url

if (page == "1")return BuildAbsolute(String.Format("Catalog.aspx?DepartmentID={0}", departmentId));elsereturn BuildAbsolute(String.Format("Catalog.aspx?DepartmentID={0}&Page={1}", departmentId, page));

 //Generate a department URL for the first page

return ToDepartment(DepartmentId, "1");

 public static string ToCategory(string departmentId, string categoryId, string page)}


if (page == "1")return BuildAbsolute(String.Format("Catalog.aspx?DepartmentID={0}&CategoryID={1}", departmentId, categoryId));elsereturn BuildAbsolute(String.Format("Catalog.aspx?DepartmentID={0}&CategoryID={1}&Page={2}", departmentId, categoryId, page));

 public static string ToCategory(string departmentId, string categoryId)}


return ToCategory(departmentId, categoryId, "1");

 public static string ToProduct(string productId)}


return BuildAbsolute(String.Format("Product.aspx?ProductId{0}", productId));

 public static string ToProductImage(string fileName)}


//build product URLreturn BuildAbsolute("/ProductImages/" + fileName);
public static string ToDepartment(string DepartmentId){
public static string ToDepartment(string departmentId, string page)}
//Get current Uri
Uri uri = HttpContext.Current.Request.Url;//Build absolute path

relativeUri = relativeUri.TrimStart(
string app = HttpContext.Current.Request.ApplicationPath;if (!app.EndsWith("/")) app += "/";'/');//Return the absolute pathreturn HttpUtility.UrlPathEncode(String.Format("http://{0}:{1}{2}{3}", uri.Host, uri.Port, app, relativeUri));

No comments:

Post a Comment

Thank you for your thoughts. Your comment will appear in my blog shortly after review.

Have a great day!