2011-03-21 34 views
18

我试图设置一个使用区域的MVC3解决方案,但我想让我的区域位于不同的程序集中。例如,我想要一个包含母版页,样式表,脚本,登录页面等共享资源的父程序集。但是我想在独立的程序集中使用不同的业务功能区域。ASP.NET MVC3 - 独立程序集中的区域

我试过这个样本是为MVC2预览版编写的:http://msdn.microsoft.com/en-us/library/ee307987%28VS.100%29.aspx。 (注意,我最初从这个堆栈溢出线程找到了这个:ASP.NET MVC - separating large app)。但似乎MVC3没有选择将视图文件移动到主项目中。我对使用嵌入式资源/ VirtualPathProvider选项并不着迷。

关于如何使用MVC3工作的任何建议?

感谢, 跳过

+0

此SO帖子可能是一个更好的解决方案为您:http://stackoverflow.com/questions/4241399/asp-net-mvc-3-rc-arearegistration-registerallareas-and-dynamically-loaded-assem – gidmanma 2011-07-13 02:16:52

回答

2

你可以单独的控制器和视图,而不使用领域。对于控制器,您可以使用Windsor或任何其他IoC容器来解析来自不同程序集的控制器。 例如,你可以用这种方式所有的控制器进行注册:

container.Register(AllTypes.FromAssemblyInDirectory(new AssemblyFilter(HttpRuntime.BinDirectory)).BasedOn<IController>().Configure(c => c.LifeStyle.Transient)); 

你也需要实现的IDependencyResolver然后设置DependencyResolver.SetResolver(...)。

有关的观点,你有两个选择:

  1. 嵌入式资源的VirtualPathProvider
  2. 简单复制的视图文件来生成后的适当位置/部署

我们建立了一个简单的框架(类似于Portable Areas)使用Windsor和由VirutalPathProvider实现提供的嵌入式资源视图。

+0

这是代码在GitHub上偶然? – cecilphillip 2013-07-17 14:58:37

+0

对不起,但我不能分享该代码,因为它属于我以前的员工。顺便说一句,我可以告诉你,最后我们放弃了嵌入的意见,但控制器仍然在分离的组件。你对这个解决方案有什么具体的部分感兴趣? – 2013-07-18 07:42:07

+0

嵌入视图和virtualPathProvider – cecilphillip 2013-07-18 17:46:14

2

您可以使用MvcContrib with Portable Areas,但这样您就可以嵌入视图。

只需创建一个MVC和一个类库项目。在MVC项目中创建您的区域,完成后将除区域中的视图之外的所有内容移动到类库中。

使用NuGet来获得这个打包,并且你可以在每个MVC项目中使用你的新NuGet区域。

11

1 - 独立您的mvc地区进入differrent的mvc项目被编译成自己单独的组件

2 - 添加到您的程序集信息。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/{1}/{0}.vbhtml", 
      "~/Areas/{2}/Views/Shared/{0}.cshtml", 
      "~/Areas/{2}/Views/Shared/{0}.vbhtml" 
     }; 

     var areaViewAndPartialViewLocationFormats = new List<string> 
     { 
      "~/Areas/{2}/Views/{1}/{0}.cshtml", 
      "~/Areas/{2}/Views/{1}/{0}.vbhtml", 
      "~/Areas/{2}/Views/Shared/{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/{1}/{0}.vbhtml", 
      "~/Views/Shared/{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/

+0

CAn你请让我知道,如果这将与aspx页面一起工作也 – Saravanan 2015-04-20 15:24:48

0

this article如何创建工作作为另一个MVC应用程序内的区域的一个项目。 基本上,区域项目中的文件位于主项目的Area文件夹下,但不作为主项目的一部分(未在项目文件中引用)。