blog

Home / DeveloperSection / Blogs / URL Routing in asp.net

URL Routing in asp.net

shreesh chandra shukla3047 27-Jul-2013

In this blog I am trying to explore the concept of  URL Routing ,and how it work it.

URL Routing is a feature newly introduced in ASP.NET that allows you to configure your application to define user friendly URLs. This helps much in search engine optimization. Search Engine Optimization (commonly known as SEO) is a strategy used to increase traffic to a website. We could also have our own custom URL route handler or some third party solution, but they have never been strong enough for an application. URL routing allows you to configure an application to accept request URLs that do not map to physical files. A request URL is simply the URL a user enters into their browser to find a page on your web site.

You can use routing to define URLs that don't map to any particular physical file on the disk. Also, you can make use of routing in ASP.NET 4 to define custom routes. According to MSDN, "Routing is fundamentally about decomposing a URL endpoint into parameters and then using those parameters to steer the HTTP request processing to a specific component.

Do you think about, “Why this .aspx extension does displays in the URL" and you thinking about removing like these long URLs.this is the normal behavior of ASP.NET application. But we can change it with our own custom solution. Now Microsoft introduced URL Routing with ASP.NET 4.0. URL routing is fully integrated, very powerful and straightforward.

 Understanding what is URL Routing:
 

We access our web application using some URL that is normally the physical path of the pages. So URL Routing is a way to provide our own URL in lieu of the physical path of the page. One other way, Routing allows us a way to configure our application to accept a requested URL which actually doesn't map to physical files. From the security perspective of the application, it's important because one can easily know the solution structure of the application. Let’s see how it behaves:

steps are given below:
  •   User request with rout URL.
  •   Routing handler invokes and identify if there is mapping for the coming rout and if yes, then request pass to the handler.
  •   Now page can access any parameter and render itself accordingly.

Why URL Routing

URL is now considered as a part of User Interface (UI) and needs proper attention. Some of the advantages are:

1-  Help in SEO (Search Engine Optimization) to  improve  the page hits by putting relevant keywords in the URL.(very commonly used technique in today’s heavier internet popularity  )

2-   It is also very handy for Users to becoming more techs savvy and finds directly manipulating the URL easier than inputting text boxes and clicking buttons.

3-  Short and easy to type URLs are good.

4-  URLs are persistent i.e. users do not want to rely on your page names and folder structure when application is accessed. For e.g. if a user bookmarks a page and for some reasons developer need to restructure the code files. User might not get to the same page through the same bookmark.

As from all these points, we can see the URLs as a part of user interface and it should be simple and easy. URLs should also make us visualize the structure of our application which might be a security concern for us, etc;

Also URL routing is very helpful in SEO (Search engine optimization) and improve the page hits but put in the appropriate keywords.

Routes

A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. A handler can also be a class that processes the request. To define a route, you create an instance of the Route class by specifying the URL pattern, the handler, and optionally a name for the route.

And you add the route to the application by adding the Route object to the static Routes property of the RouteTable class. The Routes property is a RouteCollection object that stores all the routes information for the application.

URL Patterns

A URL pattern can contain literal values and variable placeholders (referred to as URL parameters). The literals and placeholders are located in segments of the URL which are delimited by the slash (/) character.

When a request to your web application is made, the URL is parsed into segments and placeholders, and the variable values are provided to the request handler. This process is similar to the way the data in a query string is parsed and passed to the request handler. In both cases, variable information is included in the URL and passed to the handler in the form of key-value pairs. For query strings, both the keys and the values are in the URL. For routes, the keys are the placeholder names defined in the URL pattern, and only the values are in the URL.

In a URL pattern, you define placeholders by enclosing them in braces ({ and } ). You can define more than one placeholder in a segment, but the placeholders must be separated by a literal value. For example, {product}-{category}/ {action} is a valid route pattern. However, {product}{category}/{action} is not a valid pattern, because there is no literal value or delimiter between the placeholders. Therefore, routing cannot determine where to separate the value for the product placeholder from the value for the category placeholder.

The following table shows valid route patterns and examples of URL requests that match patterns

.

 


Updated 18-Sep-2014

Leave Comment

Comments

Liked By