核心:

主要利用MVC的区域功能,实现项目模块独立开发和调试。

目标:

各个模块以独立MVC应用程序存在,即模块可独立开发和调试。

动态注册各个模块路由。

一:新建解决方案目录结构

如图:

二:EasyMvc.Core即为核心库。

核心库三大主力:AreaConfig 、RouteConfig 、FilterConfig

AreaConfig :为区域启动停止以及其他状态时注入的方法,类似与Global.asax里面Application_Start、Application_End方法。

RouteConfig :路由方法,类似与App_Start里面RouteConfig方法。

FilterConfig:区域全局过滤器,针对当前路区域所有控制器的过滤(未实现)。

AreaConfig.cs

    public class AreaConfig
{
public virtual void Area_Start()
{ }
public virtual void Area_End()
{ }
}

RouteConfig.cs

    public class RouteConfig
{
public virtual void RegisterRoutes(AreaRoute routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public virtual void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
} private static Dictionary<string, RouteBase> _routes = new Dictionary<string, RouteBase>();
private static Dictionary<string, RouteConfig> _areas = new Dictionary<string, RouteConfig>(); #region Fields
public string Name { get; set; }
public string Path { get; set; }
public string[] NameSpaces { get; set; }
#endregion #region Route
public string FormatName(string name)
{
if (string.IsNullOrEmpty(name))
throw new RouteException("路由名称为空", Name);
return string.Format("{0}_{1}", Name, name);
}
public string FormatUrl(string url)
{
if (string.IsNullOrEmpty(url))
throw new RouteException("路由地址为空", Name);
return string.Format("{0}/{1}", Path, url);
}
public string[] FormatNameSpaces(string[] namespaces)
{
if (namespaces != null && namespaces.Length > )
{
List<string> list = NameSpaces == null ? (new List<string>()) : NameSpaces.ToList();
foreach (var item in namespaces)
{
if (!list.Contains(item))
list.Add(item);
}
NameSpaces = list.ToArray();
}
return null;
}
public void AddRoute(string routeName, RouteBase route)
{
if (!string.IsNullOrEmpty(routeName) && route != null)
_routes.Add(routeName, route);
} public void CheckName(string routeName)
{
if (_routes.Any(op => op.Key == routeName))
throw new RouteException("路由名称已存在", Name, routeName);
}
#endregion #region Area
public void Init()
{
Regist(RouteTable.Routes);
}
private void Regist(RouteCollection routes)
{
if (_areas.ContainsKey(Name))
throw new AreaExcption("区域已存在", Name);
_areas[Name] = this;
AreaRegistrationContext context = new AreaRegistrationContext(Name, routes);
AddNameSpaces(context);
RegisterArea(context);
if (Config.MConfig.IsDebug)
{
RegisterRoutes(routes);
}
}
private void AddNameSpaces(AreaRegistrationContext context)
{
if (NameSpaces != null && NameSpaces.Length > )
foreach (string item in NameSpaces)
{
context.Namespaces.Add(item);
}
}
private void RegisterArea(AreaRegistrationContext context)
{
AreaRoute route = new AreaRoute(this, context);
RegisterRoutes(route);
}
#endregion
}

FilterConfig.cs(未实现)

public class FilterConfig { }

三:模块重写三大核心类

App_Satrt下面的几个类,就是重写EasyMvc.Core的三大核心类的了。

AreaConfig.cs
    public class AreaConfig : EasyMvc.Core.AreaConfig
{
public override void Area_Start()
{ }
public override void Area_End()
{ }
}
RouteConfig.cs
    public class RouteConfig : EasyMvc.Core.RouteConfig
{
public override void RegisterRoutes(EasyMvc.Core.Routes.AreaRoute routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public override 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 }
);
}
}

FilterConfig.cs

public class FilterConfig : EasyMvc.Core.FilterConfig { }

四:模块配置

主项目和各个模块都可配置Module.config

<?xml version="1.0" encoding="utf-8"?>
<Modules>
<IsDebug>false</IsDebug>
<Module>
<Provider>ModuleOne</Provider>
<AreaType>ModuleOne.AreaConfig</AreaType>
<RouteType>ModuleOne.RouteConfig</RouteType>
<FilterType />
<Name>ModuleOne</Name>
<Path>A</Path>
<NameSpaces>
<string>ModuleOne.Controllers</string>
</NameSpaces>
</Module>
<Module>
<Provider>ModuleTwo</Provider>
<AreaType>ModuleTwo.AreaConfig</AreaType>
<RouteType>ModuleTwo.RouteConfig</RouteType>
<FilterType />
<Name>ModuleTwo</Name>
<Path>B</Path>
<NameSpaces>
<string>ModuleTwo.Controllers</string>
</NameSpaces>
</Module>
<Module>
<Provider>MvcApplication1</Provider>
<AreaType>MvcApplication1.AreaConfig</AreaType>
<RouteType>MvcApplication1.RouteConfig</RouteType>
<FilterType />
<Name>Test</Name>
<Path>C</Path>
<NameSpaces>
<string>MvcApplication1.Controllers</string>
</NameSpaces>
</Module>
</Modules>

EasyMvc.Core AreaApplication会根据配置动态初始化执行区域方法以及各个区域的路由注册等工作。

另外AreaViewEngines会根据配置动态设置视图查找路径。

最后效果:

更多东西请查看源码,源码下面提供下载。

原文地址:http://www.cnblogs.com/deeround/p/6706683.html

源码地址:http://files.cnblogs.com/files/deeround/EasyMvc.rar

EasyMvc--让MVC区域开发更Easy(提供源码下载)的更多相关文章

  1. 基于Android开发的天气预报app(源码下载)

    原文:基于Android开发的天气预报app(源码下载) 基于AndroidStudio环境开发的天气app -系统总体介绍:本天气app使用AndroidStudio这个IDE工具在Windows1 ...

  2. Harmony OS 开发避坑指南——源码下载和编译

    Harmony OS 开发避坑指南--源码下载和编译 本文介绍了如何下载鸿蒙系统源码,如何一次性配置可以编译三个目标平台(Hi3516,Hi3518和Hi3861)的编译环境,以及如何将源码编译为三个 ...

  3. 玩转控件:重绘DEVEXPRESS中DateEdit控件 —— 让DateEdit支持只选择年月 (提供源码下载)

      前言 上一篇博文<玩转控件:重绘ComboBox —— 让ComboBox多列显示>中,根据大家的回馈,ComboBox已经支持筛选了,更新见博文最后最后最后面.   奇葩 这两天遇到 ...

  4. 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载

    上篇博文我们介绍了mybatis的基本概念与原理,这篇博文我们通过Spring与Mybatis集成,开发一个简单用户增删改查的Web项目. 基本准备工作 1.安装JDK1.6以上版本,安装与配置 2. ...

  5. 重磅来袭,开源Asp.Net MVC网上商城BrnShop正式发布,提供源码下载(转)

    BrnShop网上商城是以Asp.Net mvc3为基础开发的网上商城,源代码完全开源(企业版的源代码目前还没有完全整理完成,一旦整理完成也全部开源). 啥话也不说了,直接上源码:下载源码(由于公司服 ...

  6. openwrt开发笔记一:源码下载与编译

    1.1 环境要求 编译系统:Linux发行版(本文使用Ubuntu) 编译一个可以安装的OpenWrt固件镜像文件(大约8MB大小的),你需要: 一个纯净的OpenWrt编译系统大约需要200MB的空 ...

  7. C编程风格的人机交互 -- CSHELL (提供源码下载)

    记得上大学时,做C语言的程序都是用sdb来调试的:再后来有了gdb,同sdb差不多,不过就好用了很多.但终究还是有点遗憾.比如,程序里设计了几个函数,如果想测试下它们,就不得不再编写个测试函数,用各种 ...

  8. 最新版本的Struts2+Spring4+Hibernate4三大框架整合(截止2014-10-15,提供源码下载)

    一. 项目名称:S2316S411H436 项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 + Quartz2.2.1 源代码下载地址: 基本版:ht ...

  9. Android 上实现像微信一样的用Fragment来实现的Tab切页效果 提供源码下载

    网有不少的例子,但是要么是像微信一样可是没有使用Fragment实现,要么是只实现了一个很简单的切换,没有下面的菜单页.这个例子有实现了,我觉得暂时够我用了##实现类:+ MainTabFragmen ...

随机推荐

  1. Android 自定义 View 圆形进度条总结

    Android 自定义圆形进度条总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 微信公众号:牙锅子 源码:CircleProgress 文中如有纰漏,欢迎大家留言指出. 最近 ...

  2. 剑指offer编程题Java实现——面试题12打印1到最大的n位数

    题目:打印1到最大的n位数 输入数字n,按顺序打印输出从1到最大的n位十进制数,比如输入3,打印从1到999. 这道题考察的地方是如何表示大数问题.由于n是任意大的数组,如果n太大的话n位数就超过了l ...

  3. ARZhu的数论初步

    数论 2017年3月4日02:11:35 gcd 1. 原理: gcd( a, b ) = gcd( b, a - b ) -> gcd( a, b ) = gcd( b, b % a ) 2. ...

  4. Sass使用小技巧

    1.任何可以用作css属性值的赋值都可以用作sass变量值.如果变量值与属性不匹配,sass会当作普通字符串来处理. $family: "microsoft yahei", Ari ...

  5. js substr和substring的区别

    在js中substring和substr都是用来截取字符串的,substr函数和substring函数都是用来从某个“母字符串”中提取“子字符串”的函数.但用法有些差别,下面分别介绍但是它们还是有区别 ...

  6. HTML 5入门知识(五)

    本地存储Web Storage 使用HTML 5的Web Storage功能,可以在客户端存储更多的数据,而且可以实现数据在多个页面中共享甚至是同步. cookie存储数据的不足 cookie可用于在 ...

  7. 初写Python

    #!/usr/bin/env python (指定用什么解释器运行脚本以及解释器所在的位置) # -*- coding:utf-8 -*- (用来指定文件编码为utf-8的)   # 无效的内容,只做 ...

  8. DotNet友元程序集解析

    项目开发的过程中,调试使用的可能是最多的操作.任何代码写出来都需要经过调试和整合,以此扩展和提升程序的稳定性和可靠性.谈到.NET的单元测试,在这里就得提提.NET的友元程序集这一特性,也借用.NET ...

  9. Golang版protobuf编译

    官方网址: https://developers.google.com/protocol-buffers/ (需要FQ) 代码仓库: https://github.com/google/protobu ...

  10. Python中的日期和时间

    感觉C语言作为一门编程的入门语言还是很好的,相比较之下,Python为代表的一些语言,适合很多非计算机专业的编程入门学习. Python 日期和时间 Python 程序能用很多方式处理日期和时间,转换 ...