MVC & Entity Framework (2)-Controller, models standalone DLL

Source: Internet
Author: User
Tags naming convention

Following the previous MVC & Entity Framework (1)-the development environment, it has not been updated for a long time. Next, take a look at how to separate the controller in MVC into a class library and then reference it in a Web project. Also, by the way, try to split the entity class in models into a separate class library.

1. Controller Class Library Project

① adding a normal class library (. Net Framework4.5)

② enter the following command in the PM console the NuGet command installs the MVC reference-this is the same effect as right-clicking "Add Reference" to the project's "References".

The version number 4.0.20710.0 is specified here, primarily to match the version of the class library that was automatically referenced when the new MVC 4 Web project was created. Can be viewed in the packages.config of the Web project.

4.0. 20710.0

③ Add a Controllers folder to the class library above, and then add the class in this folder, the naming convention is Xxxcontroller. class inherits the controller, and the other notation does not differ.

④ adds a reference to the class Library project in the Web and modifies routeconfig to increase the namespaces parameter to modify the namespace of the specified controller for the registered route.

  Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Stock", action ="Index", id =urlparameter.optional}, namespaces:New string[] {"Ivan.ControllerService.Controllers" }            ); }    }

2. Models Class Library Project

Create a new Class library project, and then add two folders: Models and ViewModels. The former corresponds to the tables in the database, the latter is mainly composed of models, which facilitates the interaction between UI level and data logic layer.

Given the use of the entity Framework, enter the following PM command.

5.0

When you are finished, you can see that the project references more than two DLLs, as follows:

At this point, you can create DataContext as you mentioned in the previous article

MVC & Entity Framework (2)-Controller, models standalone DLL

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.