In this article I will explain how to hide (remove) .ASPX extension in URL in ASP.Net. There are two ways to do it namely URL Rewriting and URL Routing.
URL Rewrite works in IIS Servers while URL Routing can be implemented within ASP.Net code inside Global.asax file. URL Routing is supported in .Net 3.5 SP1 or higher frameworks.
In this article I will explain how to manipulate URLs by hiding (removing) .ASPX extension in URL using URL Routing in ASP.Net.
 
Adding Global.asax file to the project
The first thing you need to do is add Global.asax file to the ASP.Net Website project using Add New Item dialog in Visual Studio.
How to hide ( remove ) .ASPX extension in URL in ASP.Net

Note: If you are using ASP.Net Web Application project then Global.asax file should be already present.
 
For the purpose of illustrating the URL Routing in this article, I have added some ASP.Net pages to the project as show below.
How to hide ( remove ) .ASPX extension in URL in ASP.Net
 
 
Simple Routing: Hide (Remove) .ASPX extension in URL in ASP.Net using URL Routing
In order to implement URL Routing we will need to register the routes that we need to use in our application.
In order to do that I have created a method named RegisterRoutes and it has been called inside the Application_Start event inside the Global.asax file. This method will allow us to add routes to handle so that we can create our own custom URLs.
C#
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<script runat="server">
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }
   
    static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("Customers", "Customers", "~/Customers.aspx");
    }
</script>
 
VB.Net
<%@ Application Language="VB" %>
<%@ Import Namespace="System.Web.Routing" %>
<script RunAt="server">
    Private Sub Application_Start(sender As Object, e As EventArgs)
        RegisterRoutes(RouteTable.Routes)
    End Sub
 
    Private Shared Sub RegisterRoutes(routes As RouteCollection)
        routes.MapPageRoute("Customers", "Customers", "~/Customers.aspx")
    End Sub
</script>
Inside the RegisterRoutes method, I have made use of MapPageRoute method which accepts the following three parameters.
1. routeName: Name of the Route. Must be unique for each route. You can set any unique name, I have named it as Customers for better understanding.
2. routeUrl: The Route URL you want to implement. For example here we want Customers.aspx to appear as only Customers (without .ASPX extension), thus this customization has to be defined here.
3. physicalFile: The URL of the actual ASP.Net Page where the Route URL should redirect to. For this example it is Customers.aspx.
The MapPageRoute method adds the Route to the Global Route collection i.e. RouteTable.Routes.
The RegisterRoutes is called inside the Application_Start event handler so that all the routes are added the RouteTable collection when the application starts.
That’s all you need to do the remove or hide the .ASPX extension of the ASP.Net Web Page, now if you type in the URL as http://localhost:1932/RoutingCS/Customers/  or http://localhost:1932/RoutingCS/Customers both will redirect you to the customers page i.e. Customers.aspx.
 
 
Passing parameters to another page using URL Routing
In order to explain passing of parameters to from one page to another when using URL Routing, I will extend the same example by adding a GridView with some sample data to the Customers.aspx page and adding route for the page CustomerDetails.aspx.
Binding of GridView using sample data using DataTable has already been covered extensively in my article Dynamically create DataTable and bind to GridView in ASP.Net and hence I’ll not be covering it here.
The same GridView will be used here with an additional column containing a HyperLinkField which will be used to redirect to the CustomerDetails.aspx page along with the Customer ID of the corresponding record.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
    <asp:HyperLinkField Text = "View" DataNavigateUrlFormatString = "~/Customers/{0}" DataNavigateUrlFields = "Id" />
</Columns>
</asp:GridView>


How to hide ( remove ) .ASPX extension in URL in ASP.Net
I have registered a new route for the CustomerDetails page inside the Global.asax file.
C#
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<script runat="server">
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }
   
    static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("Customers", "Customers", "~/Customers.aspx");
        routes.MapPageRoute("CustomerDetails", "Customers/{CustomerId}", "~/CustomerDetails.aspx");
    }
</script>
 
VB.Net
<%@ Application Language="VB" %>
<%@ Import Namespace="System.Web.Routing" %>
<script RunAt="server">
    Private Sub Application_Start(sender As Object, e As EventArgs)
        RegisterRoutes(RouteTable.Routes)
    End Sub
 
    Private Shared Sub RegisterRoutes(routes As RouteCollection)
        routes.MapPageRoute("Customers", "Customers", "~/Customers.aspx")
        routes.MapPageRoute("CustomerDetails", "Customers/{CustomerId}", "~/CustomerDetails.aspx")
    End Sub
</script>
Above you will notice that there’s a new route added for CustomerDetails page just below the Customer’s page route.
But this route has a place holder {CustomerId} which will be replaced by actual Customer ID of the customer. Thus the URL to the CustomerDetails.aspx will now look like http://localhost:1932/RoutingCS/Customers/1 here the digit 1 is the ID of the Customer John Hammond.
 
 
Fetching parameters from URL passed from previous page using URL Routing
On the CustomerDetails page I have added a Label to display the Customer ID that was passed from the previous page.
<b>Customer Id:</b>
<asp:Label ID="lblCustomerId" runat="server" />
Now once the parameters are passed using URL Routing they can be easily accessed from the page’s RouteData Collection as shown below.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        lblCustomerId.Text = this.Page.RouteData.Values["CustomerId"].ToString();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        lblCustomerId.Text = Me.Page.RouteData.Values("CustomerId").ToString()
    End If
End Sub
 
How to hide ( remove ) .ASPX extension in URL in ASP.Net
 
Downloads