Learning ASP. NET MVC5 framework secrets note-ASP. NET routing (10), mvc5-asp.net

Source: Internet
Author: User

Learning ASP. NET MVC5 framework secrets note-ASP. NET routing (10), mvc5-asp.net
1. Default Value of the Variable

Because we have defined the default value (areacode: 010; days: 2) for the variables that define the region number and number of days in the URL template, if you want to return the weather in Beijing for the next two days, you can directly access the application root address, specify only the specific area code, or specify both the area code and the number of days.

By default, the Route attribute of RouteData returns a Route-type object, and the RouteHandler attribute returns a PageRouteHandler object. The routing variables obtained by performing route resolution for the request URL are saved in the Values attribute of the RouteData object generated, the routing variable specified for the DataTokens attribute of the Route object during Route registration is transferred to the same name variable of RouteData.

2. Constraints

We use a telephone area number to represent the corresponding city. To ensure that a valid area number is provided in the request address, we use a regular expression (0 \ d {2, 3}) to restrict it. In addition, if we can only provide weather conditions for the next three days, we can also use a regular expression ([1-3]) to constrain the variable representing the number of days in the request address. If the content in the request address does not meet the constraints of the variable segment, it means that the corresponding route object does not match.

In addition to defining a regular expression for a variable in the form of a string, you can also specify a RouteConstaint object. All RouteConstraint types implement the IRouteConstraint interface, as shown in the following code snippet. This interface has a unique method, Math, to perform the constraints test. This method has five parameters representing the current HTTP context, the current Route object, and the name of the Route variable (the key that stores the constraint object in RouteValueDictionary) replace the placeholder variable set and route direction defined in the routing template.

Public interface IRouteConstraint {bool Match (HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection )} public enum RouteDirection {IncomingRequest // processing the URLUrlGeneration of the client... // creating a URL according to the routing definition .}


The so-called "route direction" indicates whether the route check is for request matching (inbound stack) or URL generation (outbound stack), which is represented by the two enumeration values of the enumeration type RouteDirection shown above. The two core methods of the RouteBase type, GetRouteData and GetVirtualPathData, respectively use IncomingRequest and UrlGeneration as the routing direction.

The Application Programming Interface of the ASP. NET routing system defines the HttpMethodConstraint type that implements the IRouteConstraint interface. HttpMethodConstraint provides constraints on HTTP methods (such as GET, POST, PUT, and DELETE. If we use HttpMethodConstraint to set a list of allowed HTTP methods for the Route object, the HTTP methods used by the Route request must be in this list. The HTTP Method list of the allowed routes corresponds to the read-only attribute AllowedMethods of HttpMethodConstraint, Which is initialized in the constructor.

public class HttpMethodConstraint{public HttpMethodConstraint(params string[] allowedMethods);protected virtual bool Match(HttpContextBase httpContext,Route route, string parameterName,RouteValueDictionary values,RouteDirection routeDirection);public ICollection<string> AllowedMethods { get; }}

For the preceding example, we used the following method to apply an HttpMethodConstraint object to the registered Route object during Route registration. The HTTP Method list allowed by this HttpMethodConstraint object only has the unique HTTP Method POST, meaning that the registered Route object is limited to routing POST requests.

public class Global : System.Web.HttpApplication    {        protected void Application_Start(object sender, EventArgs e)        {            var defaults = new RouteValueDictionary { { "areacode", "010" }, { "days", 2 } };             var constaints = new RouteValueDictionary { { "areacode", @"0\d{2,3}" }, { "days", @"[1-3]{1}" }, { "httpMethod", new HttpMethodConstraint("POST") } };            var dataTokens = new RouteValueDictionary { { "defaultCity", "BeiJing" }, { "defaultDays", 2 } };            RouteTable.Routes.MapPageRoute("default", "{areacode}/{days}","~/weather.aspx", false, defaults, constaints, dataTokens);        }    }


Now we use the address (/010/2) that matches the registered template to access the page, it will be 404 error.

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.