easymvc--makes MVC Region development easier (source code download)

Source: Internet
Author: User


The main use of MVC area function, to achieve the project module independent development and debugging.


Each module exists as a standalone MVC application, i.e. the module can be independently developed and debugged.

Dynamically register each module route.

One: New solution directory Structure


Two: Easymvc.core is the core library.

Core library three main forces: Areaconfig, Routeconfig, Filterconfig

Areaconfig: The method that is injected for zone start stop and other states, similar to Global.asax inside Application_Start, Application_End method.

Routeconfig: Route method, similar to App_start inside Routeconfig method.

Filterconfig: Regional Global filter for filtering (not implemented) for all controllers in the current path region.


     Public class Areaconfig    {        publicvirtualvoid  Area_start ()        {        }        public  virtualvoid  area_end ()        {        }    }


     Public classRouteconfig { Public Virtual voidregisterroutes (Arearoute routes) {routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); }         Public Virtual voidregisterroutes (routecollection routes) {routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); }        Private Staticdictionary<string, routebase> _routes =Newdictionary<string, routebase>(); Private Staticdictionary<string, routeconfig> _areas =Newdictionary<string, routeconfig>(); #regionFields Public stringName {Get;Set; }  Public stringPath {Get;Set; }  Public string[] Namespaces {Get;Set; } #endregion        #regionRoute Public stringFormatName (stringname) {            if(string. IsNullOrEmpty (name))Throw NewRouteexception ("the route name is empty", Name); return string. Format ("{0}_{1}", name, name); }         Public stringFormatUrl (stringURL) {            if(string. IsNullOrEmpty (URL))Throw NewRouteexception ("The routing 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? (Newlist<string>()): Namespaces.tolist (); foreach(varIteminchnamespaces) {                    if(!list. Contains (item)) list.                ADD (item); } Namespaces=list.            ToArray (); }            return NULL; }         Public voidAddroute (stringRouteName, RouteBase route) {            if(!string. IsNullOrEmpty (routeName) && route! =NULL) _routes.        ADD (RouteName, route); }         Public voidCheckName (stringrouteName) {            if(_routes. Any (op = op. Key = =routeName)) Throw NewRouteexception ("The route name already exists", Name, routeName); }        #endregion        #regionArea Public voidInit () {regist (routetable.routes); }        Private voidregist (routecollection routes) {if(_areas. ContainsKey (Name))Throw NewAreaexcption ("Zone already exists", Name); _areas[name]= This; AreaRegistrationContext Context=NewAreaRegistrationContext (Name, routes);            Addnamespaces (context);            Registerarea (context); if(Config.MConfig.IsDebug) {registerroutes (routes); }        }        Private voidaddnamespaces (AreaRegistrationContext context) {if(Namespaces! =NULL&& namespaces.length >0)                foreach(stringIteminchnamespaces) {context.                Namespaces.add (item); }        }        Private voidRegisterarea (AreaRegistrationContext context) {Arearoute route=NewArearoute ( This, context);        RegisterRoutes (route); }        #endregion    }

FilterConfig.cs (not implemented)

Public class filterconfig {}

Three: module rewrite three core classes

APP_SATRT The following classes are the three core classes that rewrite Easymvc.core.

     Public class AreaConfig:EasyMvc.Core.AreaConfig    {        publicoverridevoid  Area_start ()        {        }          Public Override void area_end ()        {        }    }

     Public classRouteConfig:EasyMvc.Core.RouteConfig { Public Override voidregisterroutes (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 voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); }    }


Public class FilterConfig:EasyMvc.Core.FilterConfig {}

Four: module configuration

Both the main project and each module can be configured Module.config

<?xml version="1.0"encoding="Utf-8"?><modules> <IsDebug>false</IsDebug> <Module> <Provider>ModuleOne</Provider> <areatype>moduleone.areaconfig& lt;/areatype> <RouteType>ModuleOne.RouteConfig</RouteType> <filtertype/> <name>module One</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> <ar Eatype>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 the execution of the zone method and the routing registrations for each zone based on the configuration.

In addition, Areaviewengines will find the path based on the configuration dynamic settings view.

Final effect:

More things please check the source code, the source below to provide download.

Original address: http://www.cnblogs.com/deeround/p/6706683.html

Source Address: Http://files.cnblogs.com/files/deeround/EasyMvc.rar

easymvc--makes MVC Region development easier (source code download)

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.