Extension less Urls for all Web form pages using 4 lines of code

By | February 2, 2017

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.

Vishal Monpara is a full stack Solution Developer/Architect with 12 years of experience primarily using Microsoft stack. He is currently working in Retail industry and moving 1’s and 0’s from geographically dispersed hard disks to geographically dispersed user’s mind leveraging geographically dispersed team members.

Leave a Reply

Your email address will not be published. Required fields are marked *