Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of MVC engine Development and insertion system in ASP.NET

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the ASP.NET MVC engine development plug-in system example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

I. Preface

The plug-in system in my mind should be like Nop (more powerful, such as Orchard,OSGI.NET). Each plug-in module is not just a bunch of dll that implements a business interface, and then calls it using reflection or IOC technology, but a complete mvc applet. I can control the installation and disable of plug-ins in the background. The directory structure is as follows:

After being generated, it is placed in the Plugins folder under the root of the site, and each plug-in has a subfolder.

Plugins/Sms.AliYun/

Plugins/Sms.ManDao/

I'm an obsessive-compulsive lazy guy, and I don't want to copy the generated dll files to the bin directory.

II. Problems to be solved

By default, the 1.asp.net engine only loads dll in the "bin" folder, while the plug-in files we want are scattered in subdirectories under the Plugins directory.

two。 What do you do when a model is used in the view? By default, RazorViewEngine uses BuildManager to compile the view into a dynamic assembly, and then uses Activator.CreateInstance to instantiate the newly compiled object, while with the plug-in dll, the current AppDomain does not know how to parse this view that references the model because it does not exist in "bin" or GAC. To make matters worse, you won't get any error messages telling you why it doesn't work, or what the problem is. Instead, he will tell you that the file cannot be found in the View directory.

3. A plug-in is running under the site and directly overrides the plug-in's dll, which will tell you that dll is currently in use and cannot be overwritten.

4. The view file is not placed in the View directory of the site, how to load it.

Three. Net 4.0makes all this possible.

A new feature of Net4.0 is the ability to execute code before the application is initialized (PreApplicationStartMethodAttribute), which allows the application to do some work before Application_Star, for example, we can tell our mvc plug-in system where to put the dll before the application starts, do preload processing, and so on. With regard to several new features of. Net, some foreigners have blogs to introduce, click on me. About PreApplicationStartMethodAttribute, some bloggers have already written about it, click on me. The startup module of Abp should also be implemented using the feature principle of PreApplicationStartMethodAttribute, whether or not this is the case has not been seen.

IV. Solution

1. Modify the main site web.config directory so that the runtime can load not only the files in the bin directory, but also from other directories

two。 Develop a simple plug-in management class. The function of this class is to copy the dll in each subdirectory of Plugins to the folder specified in step 1 before Application_Start. In order to make demo as simple as possible, no duplicate dll is detected (for example, the ef assembly is referenced in the plug-in, and so is the main site. The dll of ef already exists in the site bin directory. There is no need to copy the dll in the plug-in to the dynamic assembly directory set above)

Using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;using System.Web;using System.Web.Compilation;using System.Web.Hosting; [assembly: PreApplicationStartMethod (typeof (Plugins.Core.PreApplicationInit), "Initialize")] namespace Plugins.Core {public class PreApplicationInit {static PreApplicationInit () {PluginFolder = new DirectoryInfo (HostingEnvironment.MapPath ("~ / plugins")); ShadowCopyFolder = new DirectoryInfo (HostingEnvironment.MapPath ("~ / plugins/temp")) } / the directory information where the plug-in is located / private static readonly DirectoryInfo PluginFolder; / the dll directory / private static readonly DirectoryInfo ShadowCopyFolder; public static void Initialize () {Directory.CreateDirectory (ShadowCopyFolder.FullName) specified when the program should be executed; / / clear the file foreach (var f in ShadowCopyFolder.GetFiles ("* .dll", SearchOption.AllDirectories)) {f.Delete () in the plug-in dll running directory } foreach (var plugin PluginFolder.GetFiles ("* .dll", SearchOption.AllDirectories) .Where (I = > i.Directory.Parent.Nameplate = "plugins") {File.Copy (plug.FullName, Path.Combine (ShadowCopyFolder.FullName, plug.Name), true) } foreach (var an in ShadowCopyFolder .GetFiles ("* .dll", SearchOption.AllDirectories) .Select (x = > AssemblyName.GetAssemblyName (x.FullName)) .Select (x = > Assembly.Load (x.FullName) {BuildManager.AddReferencedAssembly (a);}

3. How do we get the View engine to find our view? The answer is to rewrite RazorViewEngine. I adopt the way that convention is greater than configuration (assuming that our plug-in project namespace is Plugins.Apps.Sms, then the default controller namespace is Plugins.Apps.Sms.Controllers, and the folder generated by the plug-in must be / Plugins/Plugins.Apps.Sms/). By analyzing the current controller, you can know the View directory location of the current plug-in

Using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Web;using System.Web.Mvc;using System.Web.WebPages.Razor;namespace Plugins.Web {public class CustomerViewEngine: RazorViewEngine {/ defines the address where the view page is located. / / private string [] _ viewLocationFormats = new [] {"~ / Views/Parts/ {0} .cshtml", "~ / Plugins/ {pluginFolder} / Views/ {1} / {0} .cshtml", "~ / Plugins/ {pluginFolder} / Views/Shared/ {0} .cshtml", "~ / Views/ {1} / {0} .cshtml", "~ / Views/Shared/ {0} .cshtml",} Public override ViewEngineResult FindView (ControllerContext controllerContext, string viewName, string masterName, bool useCache) {string ns = controllerContext.Controller.GetType (). Namespace; string controller = controllerContext.Controller.GetType (). Name.Replace ("Controller", ""); / / indicates that it is the controller in the plug-in, and the View directory needs to deal with if (ns.ToLower (). Contains ("plugins")) {var pluginsFolder = ns.ToLower (). Replace (".cards", "") ViewLocationFormats = ReplacePlaceholder (pluginsFolder);} return base.FindView (controllerContext, viewName, masterName, useCache);} / replace the pluginFolder placeholder / private string [] ReplacePlaceholder (string folderName) {string [] tempArray = new string [_ viewLocationFormats.Length]; if (_ viewLocationFormats! = null) {for (int I = 0; I)

< _viewLocationFormats.Length; i++) { tempArray[i] = _viewLocationFormats[i].Replace("{pluginFolder}", folderName); } } return tempArray; } }} 然后在主站点的Global.asax中将Razor引擎指定为我们重写过的 4.开始制作一个插件目录,跟我们平时建立的MVC项目并没有太大区别,只是发布时需要做一些设置。 .生成路径要按照第3条的约定来写,不然会找不到视图文件 .View目录下的web.config和.cshtml文件要复制到生成目录(在文件中点右键) 3.设置引用项目中的生成属性,主程序下面已经有了的就把"复制到输出目录"设置为无,要不然拷贝到动态bin目录时会出错,可以对第2步中的那个类改造一下,加入文件比较功能,bin目录中没有的,才拷贝到动态bin目录中。 4.生成后的目录结构如下: 5.跑一下,一切正常,插件中的控制器工作正常,视图中引用了Model也没问题

The above is all the contents of the article "sample Analysis of MVC engine Development and insertion system in ASP.NET". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report