ASP Friendly URLS Brian Santacruz

How to remove the .aspx extension in ASP.NET Web Forms?

Well, as a developer I notice there were many websites where the URL does not contain the language extension that they used to build the website, for example, instead of shows “page.php” or “page.aspx” it simple shows “page”.

So, I gave myself to the task of investigating how to achieve this, because I like to maintain everything simple and not give many details about what is behind scenes, so when I found the solution I was surprised because it was simpler than I though.

All you have to do is follow the next steps to archive this.

Here is an ASP.NET WebForms project, we have two pages “Page1” and “Page2” with its respective “.aspx” extension, on “Page1” I have a button that will take the user to “Page 2” as you can see on the following screenshot.

 

ASP Friendly URLS Brian Santacruz

 

I am using

Response.Redirect("Page2.aspx");

to change to “Page2” and as well on the back instruction and results page, we will see “.aspx” extension.

 

ASP Friendly URLS Brian Santacruz

 

To get rid of this we have to install the package “Microsoft.AspNet.FriendlyUrls” using NuGet Manager on Visual Studio.

Microsoft.AspNet.FriendlyUrls

On Visual Studio…

Tools > NuGet Package Manager > Manage NuGet Package for Solution.

 

ASP Friendly URLS Brian Santacruz

 

Once we are there:

SELECT Browse Tab > Type “Microsoft.AspNet.FriendlyUrls” and Search > SELECT from the results “Microsoft.AspNet.FriendlyUrls” > CHECK Project to install > Hit Install

 

ASP Friendly URLS Brian Santacruz

 

Accept the changes on the solution

 

ASP Friendly URLS Brian Santacruz

 

Get rid of “Site.Mobile.Master” and “ViewSwitcher.ascx” we do not need them.

 

ASP Friendly URLS Brian Santacruz

 

Add a new Item, “Global.asax”

 

ASP Friendly URLS Brian Santacruz

 

and the following code on “Application_Start”, as well as its respective libraries.

RouteTable.Routes.EnableFriendlyUrls();

using System.Web.Routing;

using Microsoft.AspNet.FriendlyUrls;

 

ASP Friendly URLS Brian Santacruz

 

And… voila! as you can see we are able to access the “Page2” without using the extension”.aspx”.

 

ASP Friendly URLS Brian Santacruz

 

Do not worry if you have different pages or folders, it will route them automatically, all you have to do is just specify the name of the page and if it applies, its respective folder route.

Easy huh?

Thank you so much for reading me, this is the first post I do on my new Blog, I will continue posting more articles related to different technologies, hope it helps in your career as Developer!

Brian.