2011-03-21 81 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文件夾下,但不作爲主項目的一部分(未在項目文件中引用)。