routing-ASP. NET MVC 4 series

Source: Internet
Author: User

Software developers often pay more attention to small detail issues, especially when considering the quality and structure of the source code. Therefore, when you encounter most sites built using ASP. NET technology, it may be strange to use the following URL address:

Http://example.com/albums/list.aspx?catid=17173&genreid=33723&page=3

Since we pay more attention to the code, why not the same attention to the URL? Although it does not seem so important, it is a legitimate and widely used WEB user interface!

Understanding URLs (Uniform Resource Locator)

Usability experts urge developers to pay attention to URLs, and point out that high-quality URLs should meet the following requirements: Easy to remember and spell, short, easy to input, can reflect the site structure, "can be cracked", the user can remove the end of the URL, to reach a higher level of information architecture, lasting, can not be changed.

Traditionally, in many Web frameworks (such as ASP, JSP, PHP, ASP.), the URL represents a physical file on disk, such as the URL above we can determine that the directory structure of the site has a albums folder, and also contains a list.aspx file. This correspondence between URLs and file systems does not apply to most MVC-based WEB frameworks, which use different methods to map URLs to the method calls of a class, rather than to a physical file on disk.

The URL is written in the initials of the Uniform Resource Locator, which is an abstract concept that can refer to either a file or the result of a method call or some other content on the server.

The URI represents the Uniform resource identifier, and from a technical point of view, all URLs are URIs. A URL is an informal concept that identifies a resource by representing its primary access mechanism. Another view is that the URI is the identifier of a resource, and the URL provides specific information to get the resource.

Routing overview

There are two main uses for routing in the asp:

    1. Match incoming requests and map these requests to the Controller action.
    2. Constructs an outgoing URL that responds to actions in the controller.

Many developers prefer to compare routes with URL rewriting. Both of these methods can be used to detach the incoming URL and end the processing request. In addition, they can build "nice" URLs for SEO (searchengine Optimization,seo). However, they also make a big difference: URL rewriting is concerned with mapping a URL to another URL, such as mapping an old URL to a new URL, compared to how the route focuses on mapping the URL to a resource.

Definition of a route

Each ASP. NET MVC program requires at least one route to define how it handles requests, but usually there will always be one or more routes, and very complex programs can be dozens of or more.

The definition of a route starts with a URL pattern because it specifies a pattern that matches the route. A route can specify its URL and its default values, and can constrain the parts of the URL to provide strict control over how and when the route matches the incoming request URL.

Now clear all the code in the RegisterRoutes method, and then add a very simple route that is added as follows:

 Public Static void RegisterRoutes (routecollection routes)
{
    Routes. MapRoute ("simple""{first}/{second}/{third}");
}

The simplest form of the MapRoute method is the URL pattern that takes the route name and route. The following table shows how routes defined in the above code resolve a specified URL into a key/value pair stored in an RouteValueDictionary instance, which can help understand how routing breaks the URL into an important piece of information that is later used in the request pipeline:

Url

URL parameter value

/albums/display/123 first= "Albums" second= "Display" third= "123"
/foo/bar/baz first= "foo" second= "bar" third= "Baz"
/a.b/c-d/e-f First= "a.b" second= "c-d" third= "E-f"

routing-ASP. NET MVC 4 series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.