Routing in ASP.Net Web Form Application

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.
  1. protected void Application_Start(object sender, EventArgs e) {  
  2.     RouteTable.Routes.MapPageRoute(  
  3.         "CustomerOrder",  
  4.         "Customer/{id}/Order",  
  5.         "~/Order/CustomerOrder.aspx");  
  6.     RouteTable.Routes.MapPageRoute(  
  7.         "CustomerBill",  
  8.         "Customer/{id}/Bill",  
  9.         "~/Bill/CustomerBill.aspx");  
  10. }
Let’s discuss the route entry a little bit. The mapping URL structure will be like the following.
  1. "Customer/{id}/Order""~/Order/CustomerOrder.aspx" 
The “Customer” part is constant in the requested URL and then we will expect a place holder value, here it is “id”. The ‘id” will represent the user’s “id” or something like a primary key. Then again we have kept the constant value which is “Bill” and if the URL pattern is like the following:
 
Customer/1/Order
 
Then it will invoke the “customerBill.aspx” page which is located under the “Bill” directory. The procedure is the same for the second entry of the routing table.
 
Now, let’s implement our Order page. Have a look at the following code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace WebApp.Order {  
  9.     public class ClsCustomerOrder {  
  10.         Dictionary < intstring > Order = new Dictionary < intstring > ();  
  11.         public ClsCustomerOrder() {  
  12.             Order.Add(1, "C# Book");  
  13.             Order.Add(2, "Java Book");  
  14.             Order.Add(3, "PHP Book");  
  15.         }  
  16.         public string GetOrder(int id) {  
  17.             return Order[id];  
  18.         }  
  19.     }  
  20.   
  21.     public partial class CustomerOrder: System.Web.UI.Page {  
  22.         protected void Page_Load(object sender, EventArgs e) {  
  23.             string[] url = HttpContext.Current.Request.Url.AbsolutePath.Split('/');  
  24.             ClsCustomerOrder Order = new ClsCustomerOrder();  
  25.             Response.Write("Order by the customer is :-" + Order.GetOrder(Convert.ToInt32(url[2])));  
  26.   
  27.         }  
  28.     }  
The implementation is very simple, in the page load we are capturing the current URL and from there we are extracting the user name then we are looking up in the dictionary (which is created on another class and working as a Model in this example) to find the user’s order.
 
Now, I know this Model implementation is not proper and in a real scenario the data may code from the DB or other persistent storage area but we have implemented it to make it simple.
 
Let’s browse the URL below.
 
 
Oh, we are getting data from the dictionary and our routing is working fine.
 
Let’s implement the Billing page now. Here is the code for the Billing page which is very similar to the Order page.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace WebApp.Bill {  
  9.     public class ClsCustomerBill {  
  10.         Dictionary < intint > Order = new Dictionary < intint > ();  
  11.         public ClsCustomerBill() {  
  12.             Order.Add(1, 1000);  
  13.             Order.Add(2, 1500);  
  14.             Order.Add(3, 2400);  
  15.         }  
  16.         public int GetOrder(int id) {  
  17.             return Order[id];  
  18.         }  
  19.     }  
  20.     public partial class CustomerBill: System.Web.UI.Page {  
  21.         protected void Page_Load(object sender, EventArgs e) {  
  22.             string[] url = HttpContext.Current.Request.Url.AbsolutePath.Split('/');  
  23.             ClsCustomerBill Order = new ClsCustomerBill();  
  24.             Response.Write("Bill by the customer is :-" + Order.GetOrder(Convert.ToInt32(url[2])));  
  25.         }  
  26.   
  27.     }  
We are now browsing below the URL and it will hit the “CustomerBilling.aspx” page and we are getting the following output.
 
 

Conclusion

 
In this article, we have implemented the concept of routing in Web Form applications. If you were not aware of the previous then I hope you have enjoyed the article. Happy learning.


Similar Articles