ASP. net mvc routing (5) Preface
The previous sections explain the routing system in MVC, which is just a rough implementation process, so that you can better understand what the routing system is doing in MVC and where it is located, you cannot see the actual use of routes through fuzzy conceptual descriptions and mind maps. Here is a simple example.
Namespace definition of a route
The url rules, default values, and url parameter constraints in a route are all defined online. This article describes the namespace definition in a route.
We all know the role of routing, so that the request matches a reasonable controller name and is sent to the Controller factory to generate a controller to execute the request. However, it is inevitable that controllers with the same name will be named in the project. Let's simulate what will happen.
Environment Configuration
In the MVC project, right-click the Controllers file and choose add> controller. Name it HomeController and select an empty template. Then you don't need to worry about this controller. Do nothing. Add the following code outside the namespace where the type is located:
1 namespace AAAAA 2 { 3 public class HomeController : Controller 4 { 5 public ActionResult Index() 6 { 7 this.ViewBag.Name = this.GetType().FullName; 8 return View(); 9 }10 }11 }12 namespace BBBBB13 {14 public class HomeController : Controller15 {16 public ActionResult Index()17 {18 this.ViewBag.Name = this.GetType().FullName;19 return View();20 }21 }22 }
In this case, the simulation project references more and more Assembly and various projects over time, which may lead to repeated names. In this case, the default routing definition in the Global. asax file:
1 routes. mapRoute (2 "Default", // route name 3 "{controller}/{action}/{id}", // URL4 new {controller = "Home" with parameters ", action = "Index", id = UrlParameter. optional} // default value of the Parameter
We don't need to worry about it. If we start debugging directly, we will see the following errors,
This is not intended for anyone. What if we only need the Home controller in the AAAAA namespace at this time? Easy to do, use routing for Configuration:
1 routes.MapRoute(2 "",3 "{controller}/{action}",4 new { controller = "Home", action = "Index" },5 null,6 new string[] { "AAAAA" }7 );
You only need to define the required namespace as an array in the MapRoute method. Right-click the Index () method in the HomeController type under the AAAAA namespace and choose add View> Add, after completion, there will be an Index in the Home folder under the Views folder of the project. the cshtml file is the added View File. Open and add the following code:
1
After executing the MVC project,
Some may ask, if you want the system to search for the Controller in the AAAAA namespace, if not, search for the Controller in BBBBB, and then define the route as follows:
1 routes.MapRoute(2 "",3 "{controller}/{action}",4 new { controller = "Home", action = "Index" },5 null,6 new string[] { "AAAAA","BBBBB" }7 );
Tell friends that this is not acceptable. If there is no Home controller in AAAAA, if there is a system, it will continue to search for all types in the BBBBB namespace, the specific implementation here will be discussed in the default controller factory later.
If you want to search for only the specified namespace, the definition must be as follows:
1 Route route= routes.MapRoute(2 "",3 "{controller}/{action}",4 new { controller = "Home", action = "Index" },5 null,6 new string[] { "AAAAA" }7 );8 route.DataTokens["UseNamespaceFallback"] = false;
Here, the MapRoute () method will return a Route object. We also mentioned the Route object before, but we do not need to obtain the reference of the Route object when registering a Route, here we need to get it, and set the value of the UseNamespaceFallback key in the okens attribute set to false. This value will be passed to the Controller factory and will be discussed later.
The result is that the system will stop searching if the specified controller is not found.
Author: Jin Yuan
Source: http://www.cnblogs.com/jin-yuan/
The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.