Sunteți pe pagina 1din 2

URL Routing

This tutorial series will teach you the basics of building an ASP.NET Web Forms
application using
ASP.NET 4.5 and Microsoft Visual Studio Express 2013 for Web. A Visual Studio 20
13 project
with C# source code is available to accompany this tutorial series.
In this tutorial, you will modify the Wingtip Toys sample application to customi
ze URL routing.
Routing enables your web application to use URLs that are friendly, easier to re
member, and
better supported by search engines. This tutorial builds on the previous tutoria
l Membership
and Administration
and is part of the Wingtip Toys tutorial series.
What you'll learn:



How to register routes for an ASP.NET Web Forms application.
How to add routes to a web page.
How to select data from a database to support routes.
ASP.NET Routing Overview
URL routing allows you to configure an application to accept request URLs that d
o 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 use routing to define URLs that are semantically meaningful t
o users and that
can help with search-engine optimization (SEO).
By default, the Web Forms template includes ASP.NET Friendly URLs. Much of the b
asic routing
work will be implemented by using Friendly URLs. However, in this tutorial you w
ill add
customized routing capabilities.
Before customizing URL routing, the Wingtip Toys sample application can link to
a product
using the following URL:
http://localhost:1234/ProductDetails.aspx?productID=2
By customizing URL routing, the Wingtip Toys sample application will link to the
same product
using an easier to read URL:
http://localhost:1234/Product/Convertible%20Car
Routes
A route is a URL pattern that is mapped to a handler. The handler can be a physi
cal file, such as
an .aspx file in a Web Forms application. A handler can also be a class that pro
cesses the
request. To define a route, you create an instance of the Route class by specify
ing the URL
pattern, the handler, and optionally a name for the route.You add the route to t
he application by adding the Route object to the static Routes property
of the RouteTable class. The Routes property is a RouteCollection object that st
ores all
the routes 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 wh
ich 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 handle
r. In both cases,
variable information is included in the URL and passed to the handler in the for
m of key-value
pairs. For query strings, both the keys and the values are in the URL. For route
s, 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 sepa
rated by a
literal value. For example, {language}-{country}/{action} is a valid route patte
rn.
However, {language}{country}/{action} is not a valid pattern, because there is n
o literal
value or delimiter between the placeholders. Therefore, routing cannot determine
where to
separate the value for the language placeholder from the value for the country p
laceholder.
Mapping and Registering Routes
Before you can include routes to pages of the Wingtip Toys sample application, y
ou must
register the routes when the application starts. To register the routes, you wil
l modify the
Application_Start event handler.
1. In Solution Explorer of Visual Studio, find and open the Global.asax.cs file.
2. Add the code highlighted in yellow to the Global.asax.cs file as follows:

S-ar putea să vă placă și