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.
This article illustrates, how to manipulate URLs by hiding (removing) .ASPX extension in URL using URL Routing in
ASP.Net.
Adding Global.asax file to the project
I have made use of the following table Hobbies with the schema as follow.
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.
Simple Routing: Hide (Remove) .ASPX extension in URL in ASP.Net using URL Routing
To start with, you will need to register the routes that we need to use in our application.
Then a method named RegisterRoutes is created 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.
Inside the RegisterRoutes method, there is a MapPageRoute method which accepts the following three parameters.
1. routeName: Name of the Route. Must be unique for each route. Here it is named as Customers.
2. routeUrl: The Route URL to be implemented. 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. Here 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.
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("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("CustomerDetails", "Customers/{CustomerId}", "~/CustomerDetails.aspx")
End Sub
</script>
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, a GridView with some sample data is added to the Customers.aspx page and adding route for the page CustomerDetails.aspx.
The HTML Markup consists of:
GridView – For displaying data.
The GridView consists of three BoundField columns.
HyperLinkField – For displaying HyperLink column.
<asp:GridView ID="gvCustomers" 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>
Dynamically creating DataTable and binding to GridView in ASP.Net
Inside the Page_Load event handler, an object of
DataTable is created.
Then, three columns are added to the
DataTable Columns collection using the
AddRange method.
An Array of objects of type DataColumn is specified which will hold the name and the optional parameter Data Type i.e. the Type of the column.
Once the schema is ready i.e. all the columns are defined, some rows have been added using the Rows.Add method.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country", typeof(string))
});
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() { New DataColumn("Id", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Country", GetType(String))
})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Registering the URL Route
The new route for the CustomerDetails page also needs to be registered inside the Global.asax file.
Here, you will notice that there is 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.
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>
CustomerDetails page
HTML Markup
The HTML Markup consists of:
Label: For displaying the CustomerId.
<b>Customer Id:</b>
<asp:Label ID="lblCustomerId" runat="server" />
Fetching parameters from URL passed from previous page using URL Routing
Inside the Page_Load event handler, the parameters passed using URL Routing are accessed from the page’s RouteData collection.
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
Screenshot
Downloads