asp.net mvc5轻松实现插件式开发
在研究Nopcommece项目代码的时候,发现Nop.Admin是作为独立项目开发的,但是部署的时候却是合在一起的,感觉挺好
这里把他这个部分单独抽离出来,
主要关键点:
- 确保你的项目是MVC5 而不是MVC4或者以前的版本
至少我用MVC4没成功,而且折腾了蛮久,
- 自定义ViewEngine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace AutofacMvc5Test
{
public class MyPathProviderViewEngine:VirtualPathProviderViewEngine
{
public MyPathProviderViewEngine()
{ MasterLocationFormats = new[] {
//Admin
"~/WebAdmin/Views/{1}/{0}.cshtml",
"~/WebAdmin/Views/Shared/{0}.cshtml",
}; ViewLocationFormats = MasterLocationFormats; PartialViewLocationFormats = MasterLocationFormats;
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
IEnumerable<string> fileExtensions = base.FileExtensions;
return new RazorView(controllerContext, partialPath, null, false, fileExtensions);
//return new RazorView(controllerContext, partialPath, layoutPath, runViewStartPages, fileExtensions, base.ViewPageActivator);
} protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
IEnumerable<string> fileExtensions = base.FileExtensions;
return new RazorView(controllerContext, viewPath, masterPath, true, fileExtensions);
}
}
} - 设置编译输出目录
就是把作为插件的网站项目的Dll输出目录指定到主项目的Bin目录,这里采取的策略是设置相对目录,这里把WebAdmin当做插件项目,所以设置了他的输出目录到主项目的Bin目录

- 注册路由
这里新建了一个AdminAreaRegistration来专门负责WebAdmin相关路由的注册,好处是每个插件负责自己的事情,相互不影响,
但是这个RegisterArea什么时候执行呢,就是主网站项目调用 AreaRegistration.RegisterAllAreas();的时候,
那么AreaRegistration.RegisterAllAreas()主要干了什么?
ASP.NET MVC会遍历通过调用BuildManager的静态方法GetReferencedAssemblies得到的程序集列表,并从中找到所有AreaRegistration类型,
然后调用每个AreaRegistration类型的RegisterArea方法
注意 之所以说这是一个比较简单的方式,是因为 这个例子只是把插件项目的DLL简单的输出到主项目的Bin目录,
插件得DLL就已经可以被成功的添加到GetReferencedAssemblies列表里,
如果就想每个插件都有自己的目录那么可能需要你手动的通过BuildManager在APPStart前把每个插件DLL加入到GetReferencedAssemblies列表里,

代码下载: https://github.com/xlb378917466/SimplePlugin_asp.netmvc5.git
asp.net mvc5轻松实现插件式开发的更多相关文章
- 从零开始实现ASP.NET Core MVC的插件式开发(一) - 使用ApplicationPart动态加载控制器和视图
标题:从零开始实现ASP.NET Core MVC的插件式开发(一) - 使用Application Part动态加载控制器和视图 作者:Lamond Lu 地址:http://www.cnblogs ...
- 从零开始实现ASP.NET Core MVC的插件式开发(二) - 如何创建项目模板
标题:从零开始实现ASP.NET Core MVC的插件式开发(二) - 如何创建项目模板 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/11155 ...
- 从零开始实现ASP.NET Core MVC的插件式开发(三) - 如何在运行时启用组件
标题:从零开始实现ASP.NET Core MVC的插件式开发(三) - 如何在运行时启用组件 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/112 ...
- 从零开始实现ASP.NET Core MVC的插件式开发(四) - 插件安装
标题:从零开始实现ASP.NET Core MVC的插件式开发(四) - 插件安装 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/11260750. ...
- 从零开始实现ASP.NET Core MVC的插件式开发(五) - 插件的删除和升级
标题:从零开始实现ASP.NET Core MVC的插件式开发(五) - 使用AssemblyLoadContext实现插件的升级和删除 作者:Lamond Lu 地址:https://www.cnb ...
- 从零开始实现ASP.NET Core MVC的插件式开发(六) - 如何加载插件引用
标题:从零开始实现ASP.NET Core MVC的插件式开发(六) - 如何加载插件引用. 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/1171 ...
- 从零开始实现ASP.NET Core MVC的插件式开发(七) - 近期问题汇总及部分解决方案
标题:从零开始实现ASP.NET Core MVC的插件式开发(七) - 问题汇总及部分解决方案 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/12 ...
- 从零开始实现ASP.NET Core MVC的插件式开发(八) - Razor视图相关问题及解决方案
标题:从零开始实现ASP.NET Core MVC的插件式开发(八) - Razor视图相关问题及解决方案 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun ...
- 从零开始实现ASP.NET Core MVC的插件式开发(九) - 升级.NET 5及启用预编译视图
标题:从零开始实现ASP.NET Core MVC的插件式开发(九) - 如何启用预编译视图 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/1399 ...
随机推荐
- The current state of generics in Delphi( 转载)
The current state of generics in Delphi To avoid duplication of generated code, the compiler build ...
- php,单引号与双引号的区别
代码示例 <?php $s='666'; $s2="999"; $test = 'name{$s} - {$s2}'; $test2 = "name{$s} - { ...
- MySQL 三 通过yum源安装指定版本的mariadb
1.yum源安装指定的版本 1)准备工作 下载安装当前次新版 https://downloads.mariadb.org/ 选择rpm包,点击Repository Config ...
- java 基础--理论知识
变量分:局部变量全局变量-----------------------------------------------------变量三大特性[标识符,初始值,作用域]定义变量:语法:[访问修饰符] ...
- ansible教程
相关教程: 权威指南:http://www.ansible.com.cn/ 博客教程:https://www.w3cschool.cn/automate_with_ansible/ 视频教程: htt ...
- 利用蒙特卡洛(Monte Carlo)方法计算π值[ 转载]
部分转载自:https://blog.csdn.net/daniel960601/article/details/79121055 圆周率π是一个无理数,没有任何一个精确公式能够计算π值,π的计算只能 ...
- 微服务日志之Spring Boot Kafka实现日志收集
前言 承接上文( 微服务日志之.NET Core使用NLog通过Kafka实现日志收集 https://www.cnblogs.com/maxzhang1985/p/9522017.html ).NE ...
- 11.翻译系列:在EF 6中配置一对零或者一对一的关系【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-fi ...
- Linux pwn入门教程(1)——栈溢出基础
作者:Tangerine@SAINTSEC 原文来自:https://bbs.ichunqiu.com/thread-42241-1-1.html 0×00 函数的进入与返回 要想理解栈溢出,首先必须 ...
- Eclipse 中 Debug 时鼠标悬停无法查看变量值
问题描述:Eclipse在Debug模式下,当鼠标移动到某个变量上面时不自动显示该变量对应的值. 解决方法:在Eclipse中点击 Window->Preferences->Java-&g ...