How to detach A. NET MVC controller to a class library

Source: Internet
Author: User
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.

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.