Objective
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, So we're like controller, are we going to be 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
SOURCE header information://<copyright file= "ControllersFactory.cs" >//Copyright (c) 2014-2034 kencery.all rights.
Created by: Han Guanlong (kencery)//Creation time: 2015-6-18//</copyright> using System;
Using System.Reflection;
Using SYSTEM.WEB.MVC;
Using System.Web.Routing;
Using System.Web.SessionState; namespace Booksystem_controllers {///<summary>///rewrite the registration controller so that it can detach the controller pipe into other class libraries///<auther>///
<name>kencery</name>///<date>2015-6-18</date>///</auther>///</summary> Description: The Icontrollerfactory interface contains three methods that need to be implemented: Createcontroller,getcontrollersessionbehavior,releasecontroller/// Use: Write the following registration statement in the first line of the method RegisterRoutes in the routeconfig in the MVC App_start folder, and register in Global.asax, before you register the route/// ControllerBuilder.Current.SetControllerFactory (New Controllersfactory ("Booksystem_controllers")); Booksystem_controllers Class Library public class Controllersfactory:icontrollerfactory {private readonly string _as for controller
Semblyname; Private Readonly string _controlerdefaultnamespage;
Private Assembly _controllerassembly; <summary>///Get the assembly name of the controller///</summary> public string AssemblyName {getting {return _AssemblyName;
}///<summary>///gets the controller's default namespace///</summary> public string controlerdefaultnamespage
{get {return _controlerdefaultnamespage;}
///<summary>///Gets the Assembly instance///</summary> public Assembly controllerassembly of the assembly where the controller resides {get {return _controllerassembly?? (_controllerassembly = Assembly.Load (AssemblyName)); Load controller Information}///<summary>///constructor instantiation///</summary>///<param name= "assembly
Name ></param> Public controllersfactory (string assemblyname) {_assemblyname = assemblyname; ///<summary>///Constructor instantiation///</summary>///<param name= "AssemblyName" ></param>///<param name= "controlerdefaultnamespage" ></param> public controllersfactory (String assemb
Lyname, String controlerdefaultnamespage) {_assemblyname = assemblyname;
_controlerdefaultnamespage = Controlerdefaultnamespage; ///<summary>///Get the full name of the controller class///</summary>///<param name= "Controllername" > Controller name < /param> public string Getcontrollerfullname (string controllername) {return string. Format ("{0}.{ 1}controller ", String. IsNullOrEmpty (controlerdefaultnamespage)?
Assemblyname:controlerdefaultnamespage, controllername);
///<summary>///Gets the controller instance object, generates an empty controller without a request context object according to Controllername, creates a ControllerContext object for the controller, and then returns the controller instance </summary>///<param name= "RequestContext" ></param>///<param name= "Controllername" ;</param> public IController Createcontroller (RequestContext requestcontext, String ControllernAME) {var controller = controllerassembly.createinstance (Getcontrollerfullname (controllername)) as Controller;
if (Controller = NULL) return null; if (Controller. ControllerContext = = NULL) {controller.
ControllerContext = new ControllerContext (RequestContext, Controller); else {controller.
Controllercontext.requestcontext = RequestContext;
return controller; ///<summary>///Returns the support type of the requested session state///</summary>///<param name= "RequestContext" >< /param>///<param name= "controllername" ></param> public sessionstatebehavior getcontrollersessionb Ehavior (RequestContext RequestContext, string controllername) {var controllertype = Controllerassembly.gettype (
Getcontrollerfullname (Controllername), true, true);
var sessionstateattr = Attribute.GetCustomAttribute (Controllertype, typeof (Sessionstateattribute), false) as Sessionstateattribute; return sessionstateattr = null?
SessionStateBehavior.Default:sessionStateAttr.Behavior;
///<summary>///Release resources///</summary>///<param name= "Controller" ></param>
public void Releasecontroller (IController controller) {var iddisposable = controller as IDisposable;
if (iddisposable!= null) {iddisposable.dispose ();
}
}
}
}
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
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"}
);