Filled Under:

URL Rewriting In ASP.NET

Share

Hello I am Tapan Kumar



Introduction


While developing a website, you may need to hide the extensions of your web pages due to many reasons such as;


  •  You may not need to show the technology you are using to the internet users.

  •  You might need to mask the url name with some other appropriate names.

  •  For better SEO, I mean search engine friendly URL.

  •  For more User friendly URL.


So, here I am going to explain you the simplest way to mask your url according to your need and wish in ASP.NET.











Behind The Scene


In your solution explorer ( in visual studio ) add a folder and name it Technology ( you name it according to your choice ) and now put the webpages you want to be there inside the newly created folder.



** mind it only .aspx pages can be masked using this technique no .html page can be.



Now add a .cs ( class file ) to the root folder of your solution. name it "SiteRouteHandler.cs", and add the following code to this file.



using System;
using System.Web;
using System.Web.Routing;
using System.Web.Compilation;
using System.Web.UI;
public class SiteRouteHandler : IRouteHandler
{   
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        Page page;
        page = BuildManager.CreateInstanceFromVirtualPath(PageVirtualPath,
        typeof(Page)) as Page;
        foreach (var item in requestContext.RouteData.Values)
        {
            HttpContext.Current.Items["qparam." + item.Key] = item.Value;
        }
       return page;
    }
    public string PageVirtualPath { get; set; }
}



After adding this bunch of code and the class file you need to go tho the Global.asax.cs page and add a namespace named "using System.Web.Routing" after this you need to add the following code to the page.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;

namespace urlRewriting
{
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.Add("BlogPost", new Route("pages/corporate/",
new SiteRouteHandler() { PageVirtualPath = "~/Blogs/WebForm1.aspx" })
);
routes.Add("BlogPost1", new Route("pages/blog/", 
new SiteRouteHandler() { PageVirtualPath = "~/Test/WebForm1.aspx" })
);
routes.Add("BlogPost2", new Route("pages/technology/test/check/", 
new SiteRouteHandler() { PageVirtualPath = "~/Technology/WebForm1.
aspx" }) );
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown

}

void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs

}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started

}

void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate
 mode
// is set to InProc in the Web.config file. If session mode is set 
to StateServer
// or SQLServer, the event is not raised.

}

}
}





 Here I have masked 3 pages those are in 3 different folders Blog, Test and Technology.



The page in Blog folder is redirected to a virtual path "pages/corporate/".

The page in Test folder is redirected to a virtual path "pages/blog/".

The page in Technology folder is redirected to a virtual path "pages/technology/test/check/".



That's it, you have successfully masked your page urls to some virtual ones and search engine friendly urls.



Happy Coding...