URL Rewriting In ASP.NET 4.0/ 4.5 WebForm

In this article, you will learn about URL rewriting in ASP.NET 4.0/ 4.5 WebForm.

In this article, you will learn about how to achieve the things given below.

  • NET 4.0/ 4.5 WebForm URL Rewriting.
  • NET 4.0 WebForm URL Routing HTTP Error 404.0 - Not Found.

You will get the answers to the following questions:

  1. What is URL Routing?
  2. What things are required to implement Routing?
  3. What settings are required in Web.Config file?
  4. How to do Routing coding in Global.asax?
  5. Step by Step Implementation.

What is URL routing?

The URL is mapped to a page or to code. Routing is more important nowadays for SEO (Search Engine Optimization). With routing, you are not showing a physical file name. We can create customized URL address, as per our need.

Like offer.aspx page, we launch it again with a different name, diwalioffer, without the file name suffix, which is URL routing.

Example

www.csharpcorner.com/offer.aspx

Above url converted in diwali,

www.csharpcorner.com/diwali-offer

Now, you can see your offer.aspx will remain the same but you had created an extra link for a Diwali offer for SEO or a digital marketing point of view.

What things are required to implement routing?

In Webform, we can do URL rewriting with the help of System.Web.Routing.dll and Global.asax.

System.Web.Routing.Dll

This library is responsible for implement routing in WebForm. Give the reference of this file in the project. Check the web.config file link of DLL, which gets activated or not.

Microsoft routing documentation

https://msdn.microsoft.com/en-us/library/system.web.routing.route(v=vs.110).aspx

Global.Asax

This is the Application and Session level event registration file, where we can code for routing and other mechanisms. You can easily find a Global.asax file in the Website or Webapplication project. If it is not there, then you can insert the file by right clicking on the project and Adding new item.

What setting is required in Web.Config file?

  1. <system.webServer>  
  2.     <modules runAllManagedModulesForAllRequests="true">  
  3.       <remove name="UrlRoutingModule"/>  
  4.       <add name="UrlRoutingModule"  
  5.            type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />  
  6.     </modules>  
  7.     <handlers>  
  8.        <add name="UrlRoutingHandler"   
  9.              preCondition="integratedMode"   
  10.              verb="*"   
  11.              path="UrlRouting.axd"   
  12.              type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>  
  13.       <remove name="UrlRoutingHandler"/>  
  14.     </handlers>  
  15.   </system.webServer>   

How to do Routing coding in Global.asax?

In Global.asax file, we set the code for the Application and Session Level. This is a global Application class, where we can set the code for the events given below.

EVENT TYPES

EVENT DESCRIPTIONS

 Application Start

 This event is called, when an Application is getting started.

 Application End

 This event is called before an Application ends.

 Application Error

 This event is called, when an un-handled error occurs.

 Session Start

 This event is called, when a new session starts.

 Session End

 This event called, when a session expires.

Routing is an Application level event and not a session level event. In an Application start event, we register routes collection.


With the above screenshot, you can see routes.MapPageRoute collection, which requires three parameter values.

Parameter Type

Value Assigned

Route Name

myfriend1

Route URL

csharpcorner-friend-list

Physical File

FriendList.aspx

Example 

  1. routes.MapPageRoute("myfriend1""csharpcorner-friend-list""~/FriendList.aspx");  
  2. //Route Name   : myfriend1   
  3. //Route URL    : csharpcorner-friend-list  
  4. //Physical File: FriendList.aspx  

Code of Global.asax file 

  1. <%@ Application Language="C#" %>  
  2. <%@ Import Namespace="System.Web.Routing" %>  
  3.   
  4. <script runat="server">  
  5.   
  6.     void Application_Start(object sender, EventArgs e)   
  7.     {  
  8.         // Code that runs on application startup  
  9.         RegisterRoutes(RouteTable.Routes);  
  10.     }  
  11.       
  12.     static void RegisterRoutes(RouteCollection routes)  
  13.     {  
  14.         routes.MapPageRoute("myfriend1""csharpcorner-friend-list""~/FriendList.aspx");  
  15.         //Route Name   : myfriend1   
  16.         //Route URL    : csharpcorner-friend-list  
  17.         //Physical File: FriendList.aspx  
  18.     }  
  19.   
  20.     void Application_End(object sender, EventArgs e)   
  21.     {  
  22.         //  Code that runs on application shutdown  
  23.   
  24.     }  
  25.           
  26.     void Application_Error(object sender, EventArgs e)   
  27.     {   
  28.         // Code that runs when an unhandled error occurs  
  29.   
  30.     }  
  31.   
  32.     void Session_Start(object sender, EventArgs e)   
  33.     {  
  34.         // Code that runs when a new session is started  
  35.   
  36.     }  
  37.   
  38.     void Session_End(object sender, EventArgs e)   
  39.     {  
  40.         // Code that runs when a session ends.   
  41.         // Note: The Session_End event is raised only when the sessionstate mode  
  42.         // is set to InProc in the Web.config file. If session mode is set to StateServer   
  43.         // or SQLServer, the event is not raised.  
  44.   
  45.     }  
  46.          
  47. </script>   

Step by step implementation

Step 1

Create a new ASP.NET Empty Web Site Project named “RoutingInWebForm”.

ASP.NET

Step 2

Right click on the project title and select ADD --> Add New Item.

ASP.NET

In Add New Item dialog box, select “Global Application Class” item, which is your Global.asax file.

ASP.NET 
  1. <%@ Application Language="C#" %>  
  2. <%@ Import Namespace="System.Web.Routing" %>  
  3.   
  4. <script runat="server">  
  5.   
  6.     void Application_Start(object sender, EventArgs e)   
  7.     {  
  8.         // Code that runs on application startup  
  9.         RegisterRoutes(RouteTable.Routes);  
  10.     }  
  11.       
  12.     static void RegisterRoutes(RouteCollection routes)  
  13.     {  
  14.         routes.MapPageRoute("myfriend1""csharpcorner-friend-list""~/FriendList.aspx");  
  15.         //Route Name   : myfriend1   
  16.         //Route URL    : csharpcorner-friend-list  
  17.         //Physical File: FriendList.aspx  
  18.     }  
  19.   
  20. </script>   

NOTE

In the required code given above, you keep as its your rest default code.

Step 3

Right click on Project title and select ADD --> Reference.

ASP.NET

Select Framework Tab in Reference Manager Window.

ASP.NET

Step 4

Right click on the project title and select Add --> Add New Item.

ASP.NET

In Add New Item dialog box, select Web Form, which is your ASP.NET Web Page named ”home.aspx”.

ASP.NET

Step 5

Right click on the project title and select Add --> Add New Item.

ASP.NET

In Add New Item dialog box, select “Web Form” item, which is your ASP.NET Web Page named FriendList.aspx.

ASP.NET

Code Home.aspx 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.      <h1>My Dashboard</h1>  
  13.           
  14.         <br />  
  15.   
  16.         <a href="csharpcorner-friend-list"> Click here for C# Corner Friend List</a>  
  17.     </div>  
  18.     </form>  
  19. </body>  
  20. </html>  

Code FriendList.aspx 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="FriendList.aspx.cs" Inherits="FriendList" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <h1>Welcome To C-Sharpcorner Friends List</h1>  
  13.         <br />  
  14.         <br />  
  15.         <table border="1">  
  16.             <tr>  
  17.                 <th>  
  18.                     Name  
  19.                 </th>  
  20.                 <th>  
  21.                     City  
  22.                 </th>  
  23.             </tr>  
  24.             <tr>  
  25.                 <td>  
  26.                     Ashish Kalla  
  27.                 </td>  
  28.                 <td>  
  29.                     Malad</td>  
  30.             </tr>  
  31.             <tr>  
  32.                 <td>  
  33.                     Suhana Kalla  
  34.                 </td>  
  35.                 <td>  
  36.                     Jogeshwari</td>  
  37.             </tr>  
  38.             <tr>  
  39.                 <td>  
  40.                     Mahesh Chand Sir  
  41.                 </td>  
  42.                 <td>  
  43.                     Mathura</td>  
  44.             </tr>  
  45.             <tr>  
  46.                 <td>  
  47.                     Dinesh Sir  
  48.                 </td>  
  49.                 <td>  
  50.                     Delhi</td>  
  51.             </tr>  
  52.                         <tr>  
  53.                 <td>  
  54.                     Praveen Sir  
  55.                 </td>  
  56.                 <td>  
  57.                     Noida</td>  
  58.             </tr>  
  59.         </table>  
  60.     </div>  
  61.     </form>  
  62. </body>  
  63. </html>  

Output

Page base URL

Home.aspx is a normal URL pattern and not routing base.

ASP.NET

As you hover on the link Click here for C# Corner Friend List, you can see the bottom side localhost:64190/csharpcorner-friend-list

Routing base page URL

ASP.NET

Happy Coding.