ASP.NET MVC4 project to build (a)

A separate controller

 

1, first of all to build a good project, a framework for .net4.5 + mv4 + ef6 + mysql

2, the separate controller in the primary item to another web assembly, and a controller model delete folders and the like. web stuff and leaving only view relevant.

3, the establishment Controllers folder as our controller manager at WebControllers, all controller class can be placed inside. Remember to add in references WebControllers in system.web and mvc4dll, you have to add a reference in the dependent WebControllers MyWeb years.

In order to facilitate the expansion. We can build a BaseController class outside to inherit system.web.mvc.controller. Then all the controllers then inherit BaseController. So we do not inherit directly system.web.mvc.controller. Finally, the main program routing configuration from the web, add the first four namespace parameters, let it go to WebControllers.Controllers looking controller. Finally, establish the appropriate view will be able to successfully separate controller.

 

Second, the separation region

When we need to build the region, you will find that it has established a small MVC architecture folder, folder hierarchy when the area for a long time too many, this time is very difficult to manage, so we need to separate out the area.

1, for example, we have established two regional admin and center, deleted controllers and models folders for each additional one region will be more a web.config, we move it to the next areas, all areas share a line, if there are multiple areas would put more than the webconfig deleted, in short, just enough for a stay in the areas.

2, establish a good case Controllers files in the folder WebControllers corresponding area name folder

3、注意,每增一个区域时还会多出个一个xxxxAreaRegistration.cs的类,该类就是用来声明注册区域的。我们在控制器项目中添加一个文件夹AreasRegit,然后把该类移动到文件夹中,然后修改为相应的命名空间,再为其配置好路由,和controller 一样,添加第4个参数,指定到相应文件夹下就行了。

4、建立好相应的控制器,再到视图层建立好相应的视图即可访问。注:这时我们启动项目访问区域时,可能会报错提示找不到 <add namespace="System.Web.Optimization"/>相关的引用。这是一个合并压缩CSS JS的程序集,这里我们直接到区域下的web.config里找到这一项配置,然后先注释掉它,如果需要,就用nuget安装即可。

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/hiose89/article/details/88909618