DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

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

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
  1. DZone
  2. Coding
  3. Frameworks
  4. URL Routing in ASP.NET 4.0 Web Forms

URL Routing in ASP.NET 4.0 Web Forms

Hajan Selmani user avatar by
Hajan Selmani
·
Oct. 16, 10 · News
Like (0)
Save
Tweet
Share
6.21K Views

Join the DZone community and get the full member experience.

Join For Free

As 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

ASP.NET Web application Form (document)

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: