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:
- What is URL Routing?
- What things are required to implement Routing?
- What settings are required in Web.Config file?
- How to do Routing coding in Global.asax?
- 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?
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true">
- <remove name="UrlRoutingModule"/>
- <add name="UrlRoutingModule"
- type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
- </modules>
- <handlers>
- <add name="UrlRoutingHandler"
- preCondition="integratedMode"
- verb="*"
- path="UrlRouting.axd"
- type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
- <remove name="UrlRoutingHandler"/>
- </handlers>
- </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
- routes.MapPageRoute("myfriend1", "csharpcorner-friend-list", "~/FriendList.aspx");
-
-
-
Code of Global.asax file
- <%@ 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("myfriend1", "csharpcorner-friend-list", "~/FriendList.aspx");
-
-
-
- }
-
- void Application_End(object sender, EventArgs e)
- {
-
-
- }
-
- void Application_Error(object sender, EventArgs e)
- {
-
-
- }
-
- void Session_Start(object sender, EventArgs e)
- {
-
-
- }
-
- void Session_End(object sender, EventArgs e)
- {
-
-
-
-
-
- }
-
- </script>
Step by step implementation
Step 1
Create a new ASP.NET Empty Web Site Project named “RoutingInWebForm”.
Step 2
Right click on the project title and select ADD --> Add New Item.
In Add New Item dialog box, select “Global Application Class” item, which is your Global.asax file.
- <%@ 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("myfriend1", "csharpcorner-friend-list", "~/FriendList.aspx");
-
-
-
- }
-
- </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.
Select Framework Tab in Reference Manager Window.
Step 4
Right click on the project title and select Add --> Add New Item.
In Add New Item dialog box, select Web Form, which is your ASP.NET Web Page named ”home.aspx”.
Step 5
Right click on the project title and select Add --> Add New Item.
In Add New Item dialog box, select “Web Form” item, which is your ASP.NET Web Page named FriendList.aspx.
Code Home.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h1>My Dashboard</h1>
-
- <br />
-
- <a href="csharpcorner-friend-list"> Click here for C# Corner Friend List</a>
- </div>
- </form>
- </body>
- </html>
Code FriendList.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="FriendList.aspx.cs" Inherits="FriendList" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h1>Welcome To C-Sharpcorner Friends List</h1>
- <br />
- <br />
- <table border="1">
- <tr>
- <th>
- Name
- </th>
- <th>
- City
- </th>
- </tr>
- <tr>
- <td>
- Ashish Kalla
- </td>
- <td>
- Malad</td>
- </tr>
- <tr>
- <td>
- Suhana Kalla
- </td>
- <td>
- Jogeshwari</td>
- </tr>
- <tr>
- <td>
- Mahesh Chand Sir
- </td>
- <td>
- Mathura</td>
- </tr>
- <tr>
- <td>
- Dinesh Sir
- </td>
- <td>
- Delhi</td>
- </tr>
- <tr>
- <td>
- Praveen Sir
- </td>
- <td>
- Noida</td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Output
Page base URL
Home.aspx is a normal URL pattern and not routing base.
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
Happy Coding.