ASP. net mvc simple programming (ii) Classic Routing

Source: Internet
Author: User
Tags url example

Topic: in actual ASP. net mvc development, the URL access rule-routing definition is very important. Because any request is inseparable from the routing. By understanding it, we will be able to understand the entire process of MVC processing requests and flexibly define access rules for various pages of the system. 1.1 why do we need routing? Let's take a look at the simplest case: Example 1: routes. mapRoute (Name: "Default", Url: "{controller}/{action}/{id}", Defaults: new {controller = "Home", action = "Index ", id = UrlParameter. optional}). We know that you can access the same page through "/Home/Index/0", "/Home/Index", "/Home/", and, it is because of the existence of the route, and it also reflects ASP. net mvc routing system. 1.2 route advantages 01. shielding physical paths to improve security when we use a route, the requested URL itself cannot analyze the location of the code file in the site directory, which improves the file security to a certain extent. For example, "Home/Index/1" cannot determine the physical location of the View File in the site. 02. it is helpful to search engine optimization. We know that for a Web system running on the Internet, Web pages are easily indexed by search engines and have a high ranking, which is often an important requirement, because it is directly linked to money. URLs of different structures have different priorities for search engines. The routing system of ASP. net mvc can be used to flexibly define URL formats to meet the search engine optimization requirements. 1.3 How to define a routing rule since we know the value of a route, let's look at how to define a routing rule. 1.3.1 in Example 1, the URL parameter value in MapRoute () is "{controller}/{action}/({id}", which is called URL mode, this mode is a string that includes fixed character literal quantities and placeholders. Syntax: {placeholder 1} literal 1 {placeholder n} literal n the so-called literal is generally "/", but it can also be a string, such as "pageIndex. ASP. net mvc matches the defined URL pattern with the actual URL. The following example shows the URL pattern: {language}-{country}/{action }, the matching URL is/zh-cn/display or/en-us/list. Note: 1. URL matching is case insensitive. 2. The two placeholders cannot be consecutive. {para1} {para2} is not allowed, that is, there is no literal between the two placeholders. 3. "/" is special compared with a general literal. The actual matching condition is that the URL mode is divided into segments by "/", and then the segments and URLs are matched. 1.3.2 RouteData objects are provided in the ASP. net mvc routing system. The RouteData object is used to save the route data generated by matching the URL mode with the actual URL. The route data is saved as a key-value pair. URL example for URL matching in the following routing mode: Route data {Controller}/{action}/{id}/Books/Edit/1 Key = Controller, Value = Books Key = action, value = Edit Key = id, Value = 1 the data in RouteData can be obtained in the action and view. The base class of the view and the base class of the controller encapsulate the RouteData object into a property named RouteData, the usage is as follows: // use RouteData String id = RouteData in the action method. values ["id"]; <! -In the view, use RouteData --> <% = RouteData. Values ["id"] %>. RouteData. Values is of the RouteValueDictionary type and is a dictionary structure. 1.3.3 special routing mode 1) * matching * is used to match the rest of the URL, for example, if the URL pattern "{controller}/{action}/{query}/{* plus}" matches the URL, some route data generated is as follows: route data 1/Home/Index/select/a/B Key = plus, value = a/B/Home/Index/select/a/B/c Key = plus, Value = a/B/c/Home/Index/select/Key = plus, value = 2) in greedy mode, there may be many situations when the URL mode matches the actual URL, resulting in uncertain route data. For example, if the URL mode "{filename}. {ext}" matches "/food. xml. aspx", route data in the following two cases is generated. Key = food, value = xml. aspx or key = food. xml, value = aspx in fact, only the routing data in this case will be generated. In the pattern matching process, search for the literal volume ". it will not give up after finding the first point, but greedy until the last point. This is the greedy matching rule. Extended: the greedy pattern is to match as many contents as possible that comply with the rules. 1.3.4 The default value of the route is as follows. mapRoute ("Default", "{controller}/{action}/{id}", new {action = "index", id = 0}); matched URL, for example, "/Home", "/Home/Index", "Home/Index/2", unmatched URLs, such as "/", because the Controller has no value. Note the following when a route contains the default value. 1) only providing the default value of the intermediate parameter does not work. When the URL mode contains multiple placeholders, only the default values of the intermediate Placeholders are provided. The default value does not play any role. URL mode default value does not match URL example {controller}/{action}/{id} Action = "index"/home/0/home/index/2) the default value does not work when the URL mode contains a literal value other than "/". The default value defined for the placeholder may not work. URL mode default value does not match URL example {controller}/{action} + {id}/{no} Controller = "home" Action = "index" Id = "0" no = "0" // home/index/home/index + 1/home/index + 10/12 1.3.5 route constraints sometimes, the URL may be in the format of/2013/12/15. To prevent illegal requests such as/2013/12/Hello, We can restrict the routing parameters. The Constraints parameter of an overloaded method of MapRoute () provides a value assignment for a regular expression.

Related Article

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: 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.