ASP.net C#(cSharp) 

How to remove extension .aspx/.html in asp/c# web application

If you are entering some sites particular page and enter a full link with the extension it feels annoying right? Want to remove extension?
So to solve the problem in asp/c# Web application, you can follow the below steps to get rid of this.

Follow the below steps carefully.

#1 Step: in your application root create a file called “Global.asax”. to Create right click on application project and select “Add -> New Item” then you will see something similar to the below image,

selection

#2 Step: Then select in left select “C# -> Web” and find “Global Application Class” and click open by default it will name the file as “Global.asax” in your application root folder.

#3 Step: Now on your right side Go to solution explorer and open “Global.asax.cs” by expanding ” Global.asax “

In this file inside function, application_start add below code

protected void Application_Start(object sender, EventArgs e)
{
   RegisterRoutes(RouteTable.Routes); // add this line
}

#4 Step: Then inside function “RegisterRoutes” start creating routes like mention below.

Syntax: routes.MapPageRoute(“name”, “here custom URL path”, “here original URL path”);

Note: if you have the same name inside another subfolder your name should be unique.
For example, if root contains a file name called index.aspx and also some subfolder “admin/index.aspx” then in “registerRoutes” use unique name

static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
     // create like this if your files are in root folder.
     routes.MapPageRoute("index", "index", "~/index.aspx");
            
     //if you have folder structure the do like this.
     routes.MapPageRoute("login", "manage/login", "~/manage/login.aspx");
}

Finally, try running an application by remove extension “.aspx “.

Also Read:  How to Use Multiple Forms in Single Page Using ValidationGroup

Related posts