URL Mapping and Routing

简介:

URL mapping, which is a clean, no-nonsense way to map one URL to another. URL routing, which is a slightly more elaborate but much more flexible system that performs the same task. URL mapping is an ideal way to deal with “one-off” redirection.URL routing can serve as the basis for a more sophisticated redirection system that deals with many more pages.Routing is particularly useful if you want to offer cleaner URLs so that search engines can index your website more easily and comprehensively.

URL Mapping
In some situations, you might want to have several URLs lead to the same page.

You define URL mapping in the <urlMappings> section of the web.config file. You supply two
pieces of information—the request URL (as the url attribute) and the new destination URL
(mappedUrl). Here’s an example:
<configuration>

  <system.web>

    <urlMappings enabled="true">

      <add url="~/category.aspx"

      mappedUrl="~/default.aspx?category=default" />

      <add url="~/software.aspx"

      mappedUrl="~/default.aspx?category=software" />

    </urlMappings>

    ...

  </system.web>

</configuration>

 In order for ASP.NET to make a match, the URL that the browser submits must match the URL specified in the web.config file almost exactly. However, there are two exceptions. First, the matching algorithm isn’t case sensitive, so the capitalization of the request URL is always ignored. Second, any query string arguments in the URL are disregarded. Unfortunately, ASP.NET doesn’t support advanced matching rules, such as wildcards or regular expressions.
When you use URL mapping, the redirection takes place in the same way as the Server.Transfer() method, which means no round-trip happens and the URL in the browser will still show the original request URL, not the new page. In your code, the Request.Path and Request.QueryString properties reflect the new (mapped) URL. The Request.RawUrl property returns the original, friendly request URL.

URL Routing
URL routing was originally designed as a core part of ASP.NET MVC, an alternative framework for building web pages that doesn’t use the web forms features discussed in this book. However, the creators of ASP.NET realized that routing could also help web forms developers tame sprawling sites and replace convoluted URLs with cleaner alternatives (which makes it easier for people to type them in and for search engines to index them). For all these reasons, they made the URL routing feature available to ordinary ASP.NET web form applications.

Unlike URL mapping, URL routing doesn’t take place in the web.config file. Instead, it’s implemented using code. Typically, you’ll use the Application_Start() method in the global.asax file to

register all the routes for your application.
To register a route, you use the RouteTable class from the System.Web.Routing namespace. It
provides a static property named Routes, which holds a collection of Route objects that are defined for
your application. Initially, this collection is empty, but you can your custom routes by calling the
MapPageRoute() method, which takes three arguments:
routeName: This is a name that uniquely identifies the route. It can be whatever you want.
routeUrl: This specifies the URL format that browsers will use. Typically, a route URL consists of one
or more pieces of variable information, separated by slashes, which are extracted and provided to
your code. For example, you might request a product page using a URL like  /products/4312.
physicalFile: This is the target web form —the place where users will be redirected when they use the
route. The information from the original routeUrl will be parsed and made available to this page as a
collection through the Page.RouteData property.
Here ’s an example that adds two routes to a web application when it first starts:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("product-details",
"product/{productID}", "~/productInfo.aspx");
RouteTable.Routes.MapPageRoute("products-in-category",
"products/category/{categoryID}", "~/products.aspx");
}
The route URL can include one or more parameters, which is represented by a placeholder in
curly brackets. For example, the first route shown here includes a parameter named productID. This
piece of information will be pulled out of the URL and passed along to the target page.
Here ’s a URL that uses this route to request a product with the ID FI_00345:
http://localhost:[PortNumber]/Routing/ product/FI_00345
The ASP.NET routing infrastructure then redirects the user to the productInfo.aspx page. All the
parameters are provided through the Page.RouteData property. Technically, Page.RouteData provides
a RouteData object. It ’s most useful property is the Values collection, which provides all the parameters
from the original request, indexed by name.
Here ’s an example that shows how the productInfo.aspx page can retrieve the requested product ID
from the original URL:
protected void Page_Load(object sender, EventArgs e)
{
string productID = (string)Page.RouteData.Values["productID"];
lblInfo.Text = "You requested " + productID;
}
Similarly, the second route in this example accepts URLs in this form:
http://localhost:[PortNumber]/Routing/ products/category/342
Although you can hard-code this sort of URL, there ’s a Page.GetRouteUrl() helper method that does
it for you automatically, avoiding potential mistakes. Here ’s an example that looks up a route (using its registered name), supplies the parameter information, and then retrieves the corresponding URL.
hyperLink.NavigateUrl = Page.GetRouteUrl("product-details", new {productID = "FI_00345" });
The slightly strange syntax that passes the parameter information uses a language feature called  anonymous types. It allows you to supply as few or as many parameters as you want. Technically, the
C# compiler automatically creates a class that includes all the parameters you supply and submits that
object to the GetRouteUrl() method. The final result is a routed URL that points to the FI_00345 product,as shown in the first example.
















本文转自cnn23711151CTO博客,原文链接:http://blog.51cto.com/cnn237111/468657,如需转载请自行联系原作者


相关文章
|
Web App开发 .NET
艾伟_转载:IIS URL Rewriting 和 ASP.NET routing(上)
  新版本URL-rewrite module for IIS 7.0的发布了,ASP.NET Routing组件随着.NET Framework 3.5 SP1的发布,并在.NET Framework 4.0 Beta中进一步成熟。
1142 0
|
数据采集 前端开发 .NET
艾伟_转载:IIS URL Rewriting 和 ASP.NET routing(下)
  在IIS URL Rewriting 和 ASP.NET routing(上)中,我们针对IIS URL Rewriting 和 ASP.NET routing的理念和功能分别进行分析并进行对比,在清楚了这些基本原理和异同之后,我们该如何在应用中挑选合适的URL优化方案呢?在本文中,我们将对此进行探讨分析,并例举几种两者协同应用的案例。
1414 0
|
前端开发 .NET 开发者
控制器 - URL routing HTTP module(一)
URL routing HTTP module 负责处理检查入站请求的 URL,并将它们分派到最合理的处理器上。URL routing HTTP module 也替代了旧版本的 ASP.NET URL 重写特性。
912 0