ASP. net mvc plug-in mechanism, asp. netmvc
Overview
The core of nopCommerce's plug-in mechanism is to use BuildManager. AddReferencedAssembly to add the plug-in Assembly loaded using Assembly. Load to the reference of the application domain. For specific implementation, see the relevant files in the Plugins directory of the Nop. Core Project in the nopCommerce solution. The PluginManager. cs file is the core file and contains the core code for processing ins. In the comments of nopCommerce, I thanked the Umbraco project for its reference and provided links to the main reference articles. We extract the necessary implementation code from the implementation of nopCommerce3.5 and create a simple version of the plug-in system Demo to demonstrate ASP. net mvc plug-in, the core only contains one marking interface IPlugin, a management class PluginManager contains two necessary methods Initialize and ReStart.
ArchitectureCreate a. NET solution SimplePlugin and add two class library projects: SimplePlugin. PluginCore, SimplePlugin. SimpleMvcPlugin, and SimplePlugin. Web. SimplePlugin. Web project select Empty template and select MVC reference. Set dependencies and Assembly references between projects: Add SimplePlugin. PluginCore project reference to SimplePlugin. SimpleMvcPlugin, and add SimplePlugin. PluginCore project reference to SimplePlugin. Web project. Add a System. Web reference to the SimplePlugin. PluginCore project.
1. Conventions(1) Conventions "~ /Plugins "is used as the plug-in root directory. (2) Conventions "~ /Plugins/bin is used as the runtime directory under medium trust level. The plug-in is loaded with a copy of the directory of the plug-in assembly at runtime, instead of directly loading the plug-in, the reason for loading the copy in the runtime directory is to prevent the dll file from being locked when the plug-in is upgraded or deleted.
2. Basic CodeAdd the following file to the SimplePlugin. PluginCore project: (1) IPlugin. cs: serves as the abstract interface for all plug-ins.
(2) PluginManager: loads and references ins. (3) Add the Plugins directory and Plugins/bin directory to the Simple. Web project. Add Controllers/PluginController. cs to the Simple. Web project to modify web. config and configure the probing element. Specify the trust level as the directory for running plug-ins under medium trust level.
3. Compile the plug-inUse Nuget to add MVC Assembly reference to SimplePlugin. SimpleMvcPlugin. Note that the MVC version of the plug-in is consistent with the MVC version referenced by SimplePlugin. PluginCore. (1) Add Controllers/SimpleMvcPlugin. cs implementation interface IPlugin (2) Add Controllers/SimplePluginMvcController. cs (2) Add Views/SimpleMvcPlugin/Index. cshtml (3) Copy SimplePlugin. simplePlugin. web/Views/web. config file to the root directory and Views directory of the current project. (4) set the. cshtml and. config file type to content, and set the generation to newer, then copy. (5) set SimplePlugin. SimpleMvcPlugin to False to copy the Assembly property referenced by the project to the local device. (6) set SimplePlugin. SimpleMvcPlugin to bin \ Debug \ SimpleMvcPlugin \ and bin \ Release \ SimpleMvcPlugin \
4. Manually deploy the plug-inGenerate a solution, visit the SimplePlugin. Web plug-in list page: copy the generated SimpleMvcPlugin folder to the Plugins folder of SimplePlugin. Web project, and access ~ of SimplePlugin. Web ~ /Plugin/Install access the SimplePlugin. Web plug-in list page to access ~ of SimplePlugin. Web ~ /SimpleMvcPlugin/Index page
5. Automatic deploymentSet the build path of SimplePlugin. SimpleMvcPlugin to .. \ SimplePlugin. Web \ Plugins \ SimpleMvcPlugin \ and set both Debug and Release configurations. Regenerate the solution and access the SimplePlugin. Web plug-in list page and ~ /SimpleMvcPlugin/Index page:
6. Test the intermediate trust levelSet the level Attribute of the trust element of web. config to Medium, and prohibit access to files outside the application directory. Regenerate the solution and access the SimplePlugin. Web plug-in list page and ~ /SimpleMvcPlugin/Index page. Check the Plugins/bin directory:
7. Test View File ModificationModify Index. cshtml in the directory of SimplePlugin. Web Plugins \ SimpleMvcPlugin \ Views \ SimpleMvcPlugin. View the Modification result:
8. Download Demo: Click to download