I hope you are familiar with routing in MVC applications or at least you know the basic concepts of routing. However, let’s explain routing in a few lines. The fact should be clear, this article is not dedicated to the basics of routing and its various forms but we will implement the concept of routing in Web Form applications.
Sometimes we see a misconception in developer’s minds (mostly young ones) that that routing is only possible in MVC applications. It’s not true in reality. The routing feature is a core concept of ASP.NET and since MVC is built on top of ASP.NET, we can use the concept. But I can say that (I hope most of you will agree with me) the concept of routing became popular in the era of MVC.
However, it does not matter whether the application is in MVC architecture or traditional WebForm application, we can use the concept of routing.
So, let’s create one Web Form application to implement the concept. Here I have created one project structure where I have created two directories called “Bill” and “Order” and kept two .aspx files in both of them. Depending on the route URL the .aspx file will be executed that we will see shortly.
Now, add the following code in the Application_start() event of the Global.aspx page. We can see that we made two entries in the RouteTable which is nothing but a key/value collection.

- protected void Application_Start(object sender, EventArgs e) {
- RouteTable.Routes.MapPageRoute(
- "CustomerOrder",
- "Customer/{id}/Order",
- "~/Order/CustomerOrder.aspx");
- RouteTable.Routes.MapPageRoute(
- "CustomerBill",
- "Customer/{id}/Bill",
- "~/Bill/CustomerBill.aspx");
- }
- "Customer/{id}/Order", "~/Order/CustomerOrder.aspx"
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace WebApp.Order {
- public class ClsCustomerOrder {
- Dictionary < int, string > Order = new Dictionary < int, string > ();
- public ClsCustomerOrder() {
- Order.Add(1, "C# Book");
- Order.Add(2, "Java Book");
- Order.Add(3, "PHP Book");
- }
- public string GetOrder(int id) {
- return Order[id];
- }
- }
- public partial class CustomerOrder: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {
- string[] url = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
- ClsCustomerOrder Order = new ClsCustomerOrder();
- Response.Write("Order by the customer is :-" + Order.GetOrder(Convert.ToInt32(url[2])));
- }
- }
- }

- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace WebApp.Bill {
- public class ClsCustomerBill {
- Dictionary < int, int > Order = new Dictionary < int, int > ();
- public ClsCustomerBill() {
- Order.Add(1, 1000);
- Order.Add(2, 1500);
- Order.Add(3, 2400);
- }
- public int GetOrder(int id) {
- return Order[id];
- }
- }
- public partial class CustomerBill: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {
- string[] url = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
- ClsCustomerBill Order = new ClsCustomerBill();
- Response.Write("Bill by the customer is :-" + Order.GetOrder(Convert.ToInt32(url[2])));
- }
- }
- }


Join the conversation! Your thoughts help the community grow.