The implementation of separating controller into class library in asp.net mvc

Source: Internet
Author: User
Tags constructor
This article mainly introduced the ASP.net MVC to separate the controller to the class library realization related data, needs the friend to be possible to refer to under -->

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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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>///rewrites the registration controller so that it can detach the controller pipe into other class libraries///<auther>///& lt;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: in MVC The following registration statement is written in the first line of the method RegisterRoutes in the Routeconfig in the App_start folder, and can be registered in Global.asax, before it can be placed in a registered route/// ControllerBuilder.Current.SetControllerFactory (New Controllersfactory ("Booksystem_controllers")); Booksystem_controllers Class Library public class Controllersfactory:icontrollerfactory {Private ReadOnly string _ for Controller AssemblyName; Private ReadOnly string _controlerdefAultnamespage; Private Assembly _controllerassembly;  ///<summary>///Gets the assembly name of the controller///</summary> public string AssemblyName {getting {return _assemblyname; }  ///<summary>///Gets the default namespace for the controller///</summary> public string Controlerdefaultnamespage {get {Retu RN _controlerdefaultnamespage; {///<summary>///gets Assembly instance of the assembly where the controller is located///</summary> public Assembly controllerassembly {get { Return _controllerassembly?? (_controllerassembly = Assembly.Load (AssemblyName)); Load controller Information}  ///<summary>///constructor instantiation///</summary>///<param name= "AssemblyName" ></param& Gt Public Controllersfactory (String assemblyname) {_assemblyname = assemblyname;}  ///<summary>///constructor instantiation// /</summary>///<param name= "AssemblyName" ></param>///<param name= "Controlerdefaultnamespage" ></param> public Controllersfactory (String assemblyname, String controlerdefaultnamespage) {_AssemblyName = AssemblyName; _controlerdefaultnamespage = Controlerdefaultnamespage; }  ///<summary>///Get the full name of the controller class///</summary>///<param name= "Controllername" > Controller name </param& Gt 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, and creates a ControllerContext object for this controller. Then return to 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& Gt <param name= "Controllername" ></param> public sessionstatebehavior Getcontrollersessionbehavior ( 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 R Eleasecontroller (IController Controller) {var iddisposable = controller as IDisposable; if (iddisposable!= null) {Iddis Posable. 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

?
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"});

Related Article

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.