EasyMvc -- make MVC Area Development easier (provide source code download), easymvc -- mvc

Source: Internet
Author: User

EasyMvc -- make MVC Area Development easier (provide source code download), easymvc -- mvc

Core:

It mainly uses the MVC regional function to achieve independent development and debugging of the project module.

 

Objectives:

Each module exists as an independent MVC application, that is, the module can be developed and debugged independently.

Dynamically register route entries for each module.

 

1. Create a solution directory structure

:

 

 

Ii. EasyMvc. Core is the Core library.

Core Database: AreaConfig, RouteConfig, and FilterConfig

AreaConfig: injection method when the region is started and stopped, similar to Application_Start and Application_End methods in Global. asax.

RouteConfig: the routing method, similar to the RouteConfig method in App_Start.

FilterConfig: Global filter for All controllers in the current region (not implemented ).

 

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 = "H Ome ", 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 s Tring FormatName (string name) {if (string. isNullOrEmpty (name) throw new RouteException ("route Name is empty", name); return string. format ("{0 }_{ 1}", Name, name);} public string FormatUrl (string url) {if (string. isNullOrEmpty (url) throw new RouteException ("the route address is empty", Name); return string. format ("{0}/{1}", Path, url);} public string [] FormatNameSpaces (string [] namespaces) {if (namespaces! = Null & namespaces. Length> 0) {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 ("route Name already exists", Name, routeName) ;}# endregion # region Area public void Init () {Regist (RouteTable. routes);} private void Regist (RouteCollection routes) {if (_ areas. containsKey (Name) throw new AreaExcption ("region already exists", Name); _ areas [Name] = this; AreaRegi StrationContext 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> 0) 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 (not implemented)

Public class FilterConfig {}

 

Iii. Rewrite the three core classes of the module

Several Classes under App_Satrt are the three Core classes that rewrite 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 {}

 

Iv. module configuration

Module. config can be configured for the main project and each Module.

<?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 dynamically initializes and executes the region method and route registration for each region based on the configuration.

In addition, AreaViewEngines dynamically sets the view search path based on the configuration.

 

Final effect:

 

For more information, see the source code. The source code is available for download.

Address: http://www.cnblogs.com/deeround/p/6706683.html

Source Code address: http://files.cnblogs.com/files/deeround/EasyMvc.rar

 

Related Article

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.