Friday, May 4, 2012

URL Rewriting using IHttpHandler

Below Example Explains how can we Rewrite the incoming url in asp.net.

 class UrlHandler : IHttpHandler  
   {  
     #region IHttpHandler Members  
     public bool IsReusable  
     {  
       get { return false; }  
     }  
     public void ProcessRequest(HttpContext context)  
     {  
       string vPath = context.Request.RawUrl;  
       //The hard coded value for path can be put in config file to create rules  
       int vIndexOfFolder = vPath.IndexOf("/users/", StringComparison.OrdinalIgnoreCase);  
       if (vIndexOfFolder > 0)  
       {  
         string vUserName = vPath.Substring(vIndexOfFolder + 7);  
         //remove .aspx extension  
         vUserName = vUserName.Substring(0, vUserName.Length - 5);  
         context.Response.Write("Welcome " + vUserName+" "+"<a href="+"'http://localhost:49637/About.aspx'>Click here to continue</a>");  
       }  
     }  
     #endregion  
   }  
 }  

No comments:

Post a Comment