MVC notes -- feature routing, mvc -- Routing

Source: Internet
Author: User

MVC notes -- feature routing, mvc -- Routing

  • Physical route: Put routes and controllers together, which is easier and more convenient, and can handle complex routing scenarios.
  • Traditional routing: centralized, mandatory, and defined based on the Code style.
Each MVC application requires a route to define its own request processing method. A route is the entry point of the application. First, let's take a look at the main concepts involved in routing: the routing definition starts from the URL template, because it specifies the pattern that matches the routing. The routing definition can be used as a characteristic of the controller class or operation method. A route can specify its URL and its default value. In addition, it can constrain each part of the URL and provide strict control over how and when the route matches the entered URL. After creating an MVC program, we can go to the Application_Start/RouteConfig. cs file. Because we mainly talk about feature routing this time, we can first delete all the default content in it and use the MapMvcAttributeRout () method to define it. Add [route ("")] In front of a view in HomeController. Here I use about, and the Route name is "aboutTest" [route ("aboutTest")]. public ActionResult About () {ViewBag. message = "Your application description page. "; return View ();} URL: http://localhost:8957/ Ceshi * do not write these URLs to home/ceshi. Because we have already defined the route, you can directly enter it to access the Controller route: we have seen how to add the routing feature directly to the operation method, however, many times the methods in the Controller class follow similar routing characteristics. Here we can set the special Routing Parameters of action, later, the routes under the Controller do not need to repeatedly write similar code. /// <summary> // defines the Route ing /// </summary> [Route ("{home} /{action} ")] public class HomeController: Controller {public ActionResult About () {ViewBag. message = "Your application description page. "; return View () ;}} URL: http://localhost:8957/ Home/About, of course, after using this method, you can also add feature routes to the Controller, you can also use the prefix "RoutePrefix" /// <summary> /// define the route ing -- use the prefix /// </summary> [RoutePrefix ("home" )] [Route ("{action}")] in this way, the program will automatically add the prefix of the "home" Controller. Both support overwriting, that is, specifying multiple routing rules for a controller: [Route ("")] [Route ("default")] public ActionResult Index () {return View ();} http://localhost:8957/ Home/default http://localhost:8957/ Home/both URLs can access the same controller * [Route ("{home}/{action}")]: in this way, the Controller has a specified Route at the front end, for example, if you do not need to enter the prefix of the Controller for URL access: http://localhost:8957/ Default http://localhost:8957/ The two URLs access the Index controller. If you have not added a feature route for the Controller, you must add the Controller name (prefix) for the access. For example: http://localhost:8957/ The home/About URL accesses the "About" Controller public ActionResult About () {ViewBag. message = "Your application description page. "; return View ();} route constraint: When we use a route with parameters, we will find that if we want the same route and different parameters to access different controllers, we need to use the routing constraint [Route ("ceshi/{id: double}")] public ActionResult Test1 (string id) {return View ();} URL: http://localhost:8957/ Ceshi/3.14/the image below is an inline table. I stole a lazy and cut an image. (unfinished, to be continued)

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.