ASP.NET routing

A. What is routing?

One kind of URL (Uniform Resource Locator) of the expressions, the URL is mapped to the called method.

Changing concepts :( the URL of the resource does not necessarily mean a static file on the Web server ).

 

II. Why use routing?

  • Like the standardized code indentation, this is to improve the quality of the code, is the importance of the URL;
  • Must eliminate the drawbacks of using a physical file mapping the URL;
  • Prevent injection attacks and improve security. URL content and conventional disks have a direct corresponding relationship may be determined by the URL directory structure of the site, the request is mapped to a routing operation of the controller, it is safe;
  • You can constrain each part of the URL.

 

III. How to define the route?

Creating a ASP.NET MVC project, open the application file (Global.asax.cs), you can see the call RouteConfig.RegisterRoutes method in the Application_Start method, the method is to concentrate on local control route.

F12 to define methods, it /App_Start/RouteConfig.cs file in.

A. Characteristic route

Call MapMvcAttributeRoutes () method to enable routing characteristics:

Routing characteristics, by definition, is defined by the characteristics of the route.

Add an empty Home controller in the Controllers.

1. A method of adding a characteristic Route

 

(When the URL is / Index request runs Index Method)

 

(Via /, / Index can access)

2. Routing value (dynamic routing)

It creates a placeholder in curly braces, such as: / Index / xiaoming will match to this route.

 

Question: under the following circumstances '/ Index' will match that route it?

Conclusion: static> move

 

3. The controller routing

If a method of routing a write ([Route (ControlName / ActionName)]), it would duplicate code, which can be time controller (s) to add the routing features.

The use of special routing parameters action

(Can / Home / Index, / Home / About About and are access Index Method)

 

Default parameters provided by the routing number assignment mode =

(When the request is a default request Index / Home)

By? No. route definition optional parameters

 

If by this time / Home / 1, then will be prompted to request 404, because if the URL into two, then it will put a '1' to match the action.

 

4. Constraint Route

 

This time, when you request '/ About / 2' will prompt request is not clear

Resolved through binding

(Only the first match will About When the argument is an int value)

Like this into the constraint-based routing templates called inline constraints , there are many:

 

II. Traditional routing

 Back RegisterRoutes method, clear code, add a traditional routing

controller and action parameters are special, they are mapped to a controller and method, in traditional routing parameters are necessary, no words will be reported 500 errors, suggesting that "controller" value does not include the required routing.

Traditional routing more flexible routing syntax rules

{controller}/{action}/{id}
{controller}-{action}-{id}
{controller}-{action}.{id}
...

These above are valid route.

 

MapRoute since the third parameter default values

        public  static  void the RegisterRoutes (the RouteCollection routes) 
        { 
            // map specified URL routing 
            routes.MapRoute ( " Simple " , " {Controller} / {Action} / {id} " , new new { 
                id = UrlParameter.Optional,   // Set id parameter is optional 
                action = " Index "  // set the default value of the action parameter Index 
            }); 
        }

 

 MapRoute fourth parameter value since the routing constraints

 

 

Guess you like

Origin www.cnblogs.com/MedlarCanFly/p/11402184.html