If you have an existing asp .net Web form application and trying to use routing to convert existing URLs to extension less friendly URLs for SEO, you would find lots of samples with MVC, but none of them will give you a way to convert existing Web form pages into extension less URLs . If you have couple of pages, it is hard to create a list of routes to their respective extension less URL. If you add more pages, you need to remember to add that page to the routing table. If you are like me, you would be wondering “There must be a better way”. Here is a 4 lines of code which will convert your existing page URLs to its extension less URLs.
Basic routing with optional parameters
// Copy and paste following code in Global.asax.cs
using System.Web.Routing;
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
// For all aspx files in the root folder + parameter
routes.MapPageRoute("FirstLevel", "{file}/{*params}", "~/{file}.aspx");
}
You would say “Vishal, that’s not 4 lines of code. It is x lines of code”. Well, no comments.
Routing with Subfolders without parameter
If you have a Website with subfolders, here is an extension to the above code which will work with subfolders.
// Copy and paste following code in Global.asax.cs
using System.Web.Routing;
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
// Without parameter
routes.MapPageRoute("FirstLevel", "{file}", "~/{file}.aspx");
routes.MapPageRoute("SecondLevel", "{folder}/{file}", "~/{folder}/{file}.aspx");
routes.MapPageRoute("ThirdLevel", "{folder}/{subfolder}/{file}", "~/{folder}/{subfolder}/{file}.aspx");
}
Routing with subfolders including optional parameters
If you have a website with subfolders and parameters in the webpage, here is a complex code
// Copy and paste following code in Global.asax.cs
using System.Web.Routing;
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
// Order of defining route matters. Hint - Start with deep files
// TODO: Replace CAPITAL words with your folder structure
// 3rd level subfolder 1 file + optional param
routes.MapPageRoute("3LEVEL_FOLDER_NAME", "3LEVEL_FOLDER_NAME/2LEVEL_FOLDER1_NAME/{file}/{*params}", "~/3LEVEL_FOLDER_NAME/2LEVEL_FOLDER1_NAME/{file}.aspx");
// 3rd level subfolder 2 file + optional param
routes.MapPageRoute("3LEVEL_FOLDER_NAME", "3LEVEL_FOLDER_NAME/2LEVEL_FOLDER2_NAME/{file}/{*params}", "~/3LEVEL_FOLDER_NAME/2LEVEL_FOLDER2_NAME/{file}.aspx");
// 2nd level + optional param
routes.MapPageRoute("2LEVEL_FOLDER_NAME", "2LEVEL_FOLDER_NAME/{file}/{*params}", "~/2LEVEL_FOLDER_NAME/{file}.aspx");
// root folder + optional param
routes.MapPageRoute("FirstLevel", "{file}/{*params}", "~/{file}.aspx");
}
Here you need to explicitly mention the each folder name which contains aspx files but it is better than mentioning each aspx files.
Leave a Reply