前言
软件系统中总是希望做到松耦合,项目的组织形式也是一样,本篇文章将介绍在ASP.NET CORE MVC中怎么样将Controller与主网站项目进行分离,并且对Areas进行支持。
实践
1.新建项目
新建两个ASP.NET Core Web应用程序,一个命名为:WebHostDemo 另一个名为: Web.Controllers ,看名字可以知道第一个项目是主程序项目,第二个是存放Controller类和Areas的项目。
2.修改Mvc配置
在WebHostDemo项目中修改ConfigureServices函数:
public void ConfigureServices(IServiceCollection services){
// Add framework services.
services.AddMvc(); var manager = new ApplicationPartManager();
var homeType = typeof(Web.Controllers.Areas.HomeController);
var controllerAssembly = homeType.GetTypeInfo().Assembly;
manager.ApplicationParts.Add(new AssemblyPart(controllerAssembly));
manager.FeatureProviders.Add(new ControllerFeatureProvider());