asp.net-mvc-3 – ASP.NET MVC3 – 分开的程序集中的区域
|
我正在尝试使用区域设置一个MVC3解决方案,但是我想让我的区域在不同的程序集中。例如,我想要一个包含共享资源(如母版页,样式表,脚本,登录页面等)的父程序集,但是我需要在不同程序集中具有不同的业务功能领域。 我试过这个为MVC2预览编写的示例:http://msdn.microsoft.com/en-us/library/ee307987%28VS.100%29.aspx.(注意,我最初是从这个Stack Overflow线程中找到的:ASP.NET MVC – separating large app)。但是看起来,MVC3没有选择将视图文件移动到主项目中。对于使用嵌入式资源/ VirtualPathProvider选项我不是很疯狂。 有关如何使MVC3工作的任何建议? 谢谢, 解决方法1 – 将Mvc区域分解成不同的Mvc项目,以编译到自己的单独程序集中2 – 将其添加到AssemblyInfo.cs类中,以在应用程序加载时调用方法 [assembly: PreApplicationStartMethod(typeof(PluginAreaBootstrapper),"Init")] 3 – 在加载过程中调用Init方法时,这是什么 public class PluginAreaBootstrapper
{
public static readonly List<Assembly> PluginAssemblies = new List<Assembly>();
public static List<string> PluginNames()
{
return PluginAssemblies.Select(
pluginAssembly => pluginAssembly.GetName().Name)
.ToList();
}
public static void Init()
{
var fullPluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Areas");
foreach (var file in Directory.EnumerateFiles(fullPluginPath,"*Plugin*.dll"))
PluginAssemblies.Add(Assembly.LoadFile(file));
PluginAssemblies.ForEach(BuildManager.AddReferencedAssembly);
}
}
4 – 添加自定义RazorViewEngine public class PluginRazorViewEngine : RazorViewEngine
{
public PluginRazorViewEngine()
{
AreaMasterLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml","~/Areas/{2}/Views/{1}/{0}.vbhtml","~/Areas/{2}/Views/Shared/{0}.cshtml","~/Areas/{2}/Views/Shared/{0}.vbhtml"
};
AreaPartialViewLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml","~/Areas/{2}/Views/Shared/{0}.vbhtml"
};
var areaViewAndPartialViewLocationFormats = new List<string>
{
"~/Areas/{2}/Views/{1}/{0}.cshtml","~/Areas/{2}/Views/Shared/{0}.vbhtml"
};
var partialViewLocationFormats = new List<string>
{
"~/Views/{1}/{0}.cshtml","~/Views/{1}/{0}.vbhtml","~/Views/Shared/{0}.cshtml","~/Views/Shared/{0}.vbhtml"
};
var masterLocationFormats = new List<string>
{
"~/Views/{1}/{0}.cshtml","~/Views/Shared/{0}.vbhtml"
};
foreach (var plugin in PluginAreaBootstrapper.PluginNames())
{
masterLocationFormats.Add(
"~/Areas/" + plugin + "/Views/{1}/{0}.cshtml");
masterLocationFormats.Add(
"~/Areas/" + plugin + "/Views/{1}/{0}.vbhtml");
masterLocationFormats.Add(
"~/Areas/" + plugin + "/Views/Shared/{1}/{0}.cshtml");
masterLocationFormats.Add(
"~/Areas/" + plugin + "/Views/Shared/{1}/{0}.vbhtml");
partialViewLocationFormats.Add(
"~/Areas/" + plugin + "/Views/{1}/{0}.cshtml");
partialViewLocationFormats.Add(
"~/Areas/" + plugin + "/Views/{1}/{0}.vbhtml");
partialViewLocationFormats.Add(
"~/Areas/" + plugin + "/Views/Shared/{0}.cshtml");
partialViewLocationFormats.Add(
"~/Areas/" + plugin + "/Views/Shared/{0}.vbhtml");
areaViewAndPartialViewLocationFormats.Add(
"~/Areas/" + plugin + "/Views/{1}/{0}.cshtml");
areaViewAndPartialViewLocationFormats.Add(
"~/Areas/" + plugin + "/Views/{1}/{0}.vbhtml");
areaViewAndPartialViewLocationFormats.Add(
"~/Areas/" + plugin + "/Areas/{2}/Views/{1}/{0}.cshtml");
areaViewAndPartialViewLocationFormats.Add(
"~/Areas/" + plugin + "/Areas/{2}/Views/{1}/{0}.vbhtml");
areaViewAndPartialViewLocationFormats.Add(
"~/Areas/" + plugin + "/Areas/{2}/Views/Shared/{0}.cshtml");
areaViewAndPartialViewLocationFormats.Add(
"~/Areas/" + plugin + "/Areas/{2}/Views/Shared/{0}.vbhtml");
}
ViewLocationFormats = partialViewLocationFormats.ToArray();
MasterLocationFormats = masterLocationFormats.ToArray();
PartialViewLocationFormats = partialViewLocationFormats.ToArray();
AreaPartialViewLocationFormats = areaViewAndPartialViewLocationFormats.ToArray();
AreaViewLocationFormats = areaViewAndPartialViewLocationFormats.ToArray();
}
}
5 – 从您不同的Mvc(区域)项目注册您的区域 namespace MvcApplication8.Web.MyPlugin1
{
public class MyPlugin1AreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "MyPlugin1"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MyPlugin1_default","MyPlugin1/{controller}/{action}/{id}",new {action = "Index",id = UrlParameter.Optional}
);
}
}
}
源代码和其他参考资料可以在这里找到:http://blog.longle.io/2012/03/29/building-a-composite-mvc3-application-with-pluggable-areas/ (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 为什么IIS线程这么珍贵与常规CLR线程相比?
- asp.net-mvc – ModelState.IsValid或Model.IsValid?
- asp.net – UpdatePanel中的动态创建控件?
- asp.net – 引用不稳定的DLL
- msbuild – 如何为ASP.NET 5项目设置TeamCity构建
- asp.net-mvc – 从MVC中的控制器确定部分视图的模型
- asp.net-mvc – 如何使输入字段仅允许使用EF和数据注释的数
- asp.net – 在开发,分段和生产环境之间区分web.config
- asp.net-mvc-4 – 覆盖用于MVC4应用程序的User.IsInRole和[
- asp.net – MasterPage是否知道正在显示的页面?
