Tuesday, January 4, 2011

URL Rewrite Using ASP.net 3.5 or Over




The blog will explin how we can achieve url rewriting in ASP.net 3.5 or Over.

Step1 : Create a helper class title "SiteRouteHandler.cs".

 1: #region System
 2: using System;
 3: using System.Collections.Generic;
 4: using System.Linq;
 5: using System.Web;
 6: using System.Web.UI;
 7: using System.Web.Routing;
 8: using System.Web.Compilation;
 9: #endregion
10:  
11: namespace UrlRewrite
12: {
13:  
14:    /// <summary>
15:    /// Handler for routing
16:    /// </summary>
17:    public class SiteRouteHandler : IRouteHandler
18:    {
19:  
20:       #region Properties
21:  
22:       /// <summary>
23:       /// Page Virtual Path
24:       /// </summary>
25:       public string PageVirtualPath { get; set; }
26:  
27:       #endregion
28:  
29:       #region Implements IRouteHandler
30:  
31:       /// <summary>
32:       /// Gets the handler in return
33:       /// </summary>
34:       /// <param name="requestContext"></param>
35:       /// <returns></returns>
36:       public IHttpHandler GetHttpHandler(RequestContext requestContext)
37:       {
38:  
39:          Page page = BuildManager.CreateInstanceFromVirtualPath(PageVirtualPath,typeof(Page)) as Page;
40:          foreach (var item in requestContext.RouteData.Values)
41:          {
42:  
43:             HttpContext.Current.Items["qparam." + item.Key] = item.Value;
44:          }
45:  
46:          return page;
47:       }
48:  
49:       #endregion
50:    }
51:  
52: }

Step 2 : In global.asax.cs file declare the routes you want to give to
the pages with a particular pattern. Use the SiteHandler class for getting the params
mentioned in the route pattern. The "{ID}", "{AspxPage}" and "{PageNumber}" are
the dynamic values which can be used on our aspx page for manupulation. Than according
to the pattern "123" is {ID}, "This-is-a-blog" is {AspxPage} and 32 is {PageNumber}.
"~/Blog.aspx" is the actual page.

 1:  using System.Web.Routing;
 2:  protected void Application_Start(object sender, EventArgs e)
 3:  {
 4:   
 5:     //Clears all route
 6:     RouteTable.Routes.Clear();
 7:   
 8:     //Handles the route
 9:     RouteTable.Routes.Add("Blog", new Route("blog/{ID}/{AspxPage}.aspx/{PageNumber}", new
10:                                    SiteRouteHandler() { PageVirtualPath = "~/Blog.aspx" }));
11:  }
12:   

Step 3 : Now on Blog.aspx we can use params to retrieve the data.


 1:  /// <summary>
 2:  /// The blog id from the query string
 3:  /// </summary>
 4:  private int BlogID
 5:  {
 6:   
 7:     get
 8:   
 9:     {
10:   
11:        if (Context.Items["qparam.ID"] == null)
12:        {
13:   
14:           return 0;
15:        }
16:   
17:        return Convert.ToInt32(Context.Items["qparam.ID"]);
18:     }
19:   
20:  }
21:   
22:   
23:  /// <summary>
24:  /// The blog id from the query string
25:  /// </summary>
26:  private string AspxPage
27:  {
28:   
29:     get
30:   
31:     {
32:   
33:        if (Context.Items["qparam.AspxPage"] == null)
34:        {
35:   
36:           return string.Empty;
37:        }
38:   
39:        return Context.Items["qparam.AspxPage"].ToString();
40:     }
41:   
42:  }
43:   
44:   
45:  /// <summary>
46:  /// The Page Number
47:  /// </summary>
48:  private int PageNumber
49:  {
50:   
51:     get
52:   
53:     {
54:   
55:        if (Context.Items["qparam.PageNumber"] == null)
56:        {
57:   
58:           return 0;
59:        }
60:   
61:        return Convert.ToInt32(Context.Items["qparam.PageNumber"]);
62:     }
63:   
64:  }