In the development of ASP.net mvc, after we created the project, ASP.net mvc existed in the form of Model-controller-view, the model we can easily separate into class library when creating the content automatically generated by the project, so it is not explained here, Then we are like controller is not also able to separate out. The answer is yes, let's explore how controller separates.
Here I provide two kinds of separation methods, one is to rewrite the method inherited from the Icontrollerfactory interface, the implementation of the method, and the second is that MVC directly in the routing register to control the writing of the controller, the following two types of simple paste code.
The first of these methods
The code is as follows: When the code is finished, it is registered in the route. Hint: the implementation must be completed in the routing rule Method (RegisterRoutes) inside the registration code as follows:
ControllerBuilder.Current.SetControllerFactory (New Controllersfactory ("Booksystem_controllers")); Booksystem_controllers Class Library for controller
The second method
The route Registration method code is as follows: The disadvantage is that if you have more than one rule for registering a route, you must remember to add the namespaces property, or you will get an error ?
| 1 2 3 4 5 6 7 |
//system default route routes. Maproute ( name: "Default", URL: "{controller}/{action}/{id}", defaults:new {controller = " Login ", action =" Index ", id = urlparameter.optional}, namespaces:new string[] {" Booksystem_controllers "} ); |
The latter method is simple and easy to use, the following as long as specified for the name space on it, the first one has not been tried.