URL Routing in ASP.NET 4.0 Web Forms
Join the DZone community and get the full member experience.
Join For FreeAs a capability, URL routing was first introduced in ASP.NET 3.5 SP1. The URL routing is well known in the ASP.NET MVC, but it can be also implemented in ASP.NET Web Forms Framework.
URL routing can help a lot in Search Engine Optimization (SEO).
What is it all about?
Transforming the physical path of the URL to virtual path with different semantically meaningful URL name, easier for understanding by the users.
Example:
http://www.website.com/Products.aspx?categoryId=1&productId=5
can be transformed to
http://www.website.com/Products/1/5
How we can achieve this?
Lets create an example in few steps:
1. Create new page with name Products.aspx
2. Open the Global.asax file (if not added to the project, add it – Right click on Project Name –> Add –> New Item…)
3. Add the following directive
using System.Web.Routing;
4. Inside the Application_Start method, write the following code
RouteTable.Routes.Add(
"ProductRoute",
new Route("Products/{category}/{product}",
new PageRouteHandler("~/Products.aspx")));
You can add multiple Routes inside the RouteTable for multiple ASPX page resources you have.
4. Now, open the Products.aspx code-behind file and add the following code on your Page_Load method
protected void Page_Load(object sender, EventArgs e)
{
string category = Convert.ToString(Page.RouteData.Values["category"]);
string product = Convert.ToString(Page.RouteData.Values["product"]);
//perform some operations here ;)
Response.Write(
"You have selected Product ID:"
+ product +
" from Category ID:"
+ category); //test
}
You see, there is a difference comparing with the method we are using when we
have Query Strings.
If we would have Query Strings, the following code would
be needed:
Convert.ToString(Request.QueryString["category"])
Testing the solution
1. Run your web application
2. Navigate to the Products.aspx page
Once you are there, you will see the
following message:
You have selected Product ID: from Category ID:
in the URL you have http://…/Products.aspx
3. Change the Products.aspx to
Products/1/10 and press Enter
Once you do that, you will see
the following result
You have selected Product ID:10 from Category ID:1
And that’s it ;).
So, these are the basic steps to create URL Routing in ASP.NET 4.0 Web Forms application.
Of course, there are additional tasks that you might need to accomplish when implementing complex URL Routings, for example you might not need everyone to have access to the direct resource of the page Product.aspx so that you will need to create some authorization rules, that in most of the scenarios can be easily configured in Web.config.
I hope this was helpful information.
Regards,
Hajan
Published at DZone with permission of Hajan Selmani, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Managing Data Residency, the Demo
-
Security Challenges for Microservice Applications in Multi-Cloud Environments
-
Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
-
Auto-Scaling Kinesis Data Streams Applications on Kubernetes
Comments