大型ASP.NET MVC4项目之插入式程序结构

来源:互联网 发布:重生之网络娱乐txt 编辑:程序博客网 时间:2024/05/17 15:21

一,摘要

开发松耦合的模块化程序是一个理想的设计,因为这种设计消除了重编译的风险。使用这种方式设计的模块当开发、部署的时候不需要重编译整个项目。基于一个主项目,在它之上你有多个模块,类似于wordpress core和wordpress插件。下面我们探讨下如何开发一个可插入式的ASP.NET MVC4程序结构。

二,内容

1、创建一个新的ASP.NET MVC项目,模板选择空模板。目前的项目结构如下:

2、创建一个Home控制器,以及Index视图,当运行项目的时候我们将会看如下输出:

3、创建一个Areas文件夹用来承载我们的模块,删除没用到的Models以及App_data文件夹,项目结构看起来如下:

我们的物理文件将显示如下:

4、现在我们添加我们的首个模块叫做Forum

5、在核心项目上查看所有文件将显示如下:

6、点击Forum项目,设置项目输出为..\..\bin

7、在Forum项目的App_Start文件夹下添加ForumAreaRegistration代码文件:

namespace Forum.App_Start{    public class ForumAreaRegistration : AreaRegistration    {        public override string AreaName        {            get { return "Forum"; }        }        public override void RegisterArea(AreaRegistrationContext context)        {            context.MapRoute(                "forum_default",                "Forum/{controller}/{action}/{id}",                new { action = "Index", id = UrlParameter.Optional },                namespaces: new string[] { "Forum.Controllers" }            );        }    }}

8、为了避免名称冲突,我们也要将我们的核心项目的路由配置加名称空间

namespace Core{    public class RouteConfig    {        public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                name: "Default",                url: "{controller}/{action}/{id}",                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },                namespaces: new string[] { "Core.Controllers" }            );        }    }}

9、编译整个解决方案,运行程序

模块首页

 

0 0
原创粉丝点击