2011-01-26 36 views

回答

3

看看mvc contrib的便攜式區域: http://www.lostechies.com/blogs/hex/archive/2009/11/01/asp-net-mvc-portable-areas-via-mvccontrib.aspx 它們是專門爲此目的而製作的。如果你走的是這條路,那麼你需要的代碼就更少了;-)

+0

我看過洛杉磯郵報。我相信它對人們來說效果很好,但對我所需要的東西來說這完全是過度殺傷力。公共汽車和其他配置的消息似乎傳遞給我。 Razor視圖編譯是視圖上的一個屬性設置(儘管在每個視圖上),就是這樣。 – blu 2011-01-26 02:34:00

18

我修改了發佈的想法here,與MVC3一起工作。這非常快速和簡單。唯一的小缺點是共享視圖需要嵌入資源,因此需要編譯。

  • 將共享視圖(.cshtml,.vbhtml文件)放入庫項目中。 (我在這個項目中也有一些共享控制器。)如果你想從你的應用程序中使用_Layout.cshtml,請確保你的共享視圖中包含一個指向它的_ViewStart.cshtml。

  • 在庫項目中,將所有視圖的構建操作屬性設置爲Embedded Resource。

  • 在庫項目中添加下面的代碼,將您的視圖的內容寫入tmp/Views目錄。

public class EmbeddedResourceViewEngine : RazorViewEngine 
{ 
    public EmbeddedResourceViewEngine() 
    { 
     ViewLocationFormats = new[] { 
     "~/Views/{1}/{0}.aspx", 
     "~/Views/{1}/{0}.ascx", 
     "~/Views/Shared/{0}.aspx", 
     "~/Views/Shared/{0}.ascx", 
     "~/Views/{1}/{0}.cshtml", 
     "~/Views/{1}/{0}.vbhtml", 
     "~/Views/Shared/{0}.cshtml", 
     "~/Views/Shared/{0}.vbhtml", 
     "~/tmp/Views/{0}.cshtml", 
     "~/tmp/Views/{0}.vbhtml" 
    }; 
     PartialViewLocationFormats = ViewLocationFormats; 

     DumpOutViews(); 
    } 

    private static void DumpOutViews() 
    { 
     IEnumerable<string> resources = typeof(EmbeddedResourceViewEngine).Assembly.GetManifestResourceNames().Where(name => name.EndsWith(".cshtml")); 
     foreach (string res in resources) { DumpOutView(res); } 
    } 

    private static void DumpOutView(string res) 
    { 
     string rootPath = HttpContext.Current.Server.MapPath("~/tmp/Views/"); 
     if (!Directory.Exists(rootPath)) 
     { 
      Directory.CreateDirectory(rootPath); 
     } 

     Stream resStream = typeof(EmbeddedResourceViewEngine).Assembly.GetManifestResourceStream(res); 
     int lastSeparatorIdx = res.LastIndexOf('.'); 
     string extension = res.Substring(lastSeparatorIdx + 1); 
     res = res.Substring(0, lastSeparatorIdx); 
     lastSeparatorIdx = res.LastIndexOf('.'); 
     string fileName = res.Substring(lastSeparatorIdx + 1); 

     Util.SaveStreamToFile(rootPath + fileName + "." + extension, resStream); 
    } 
} 

我使用阿德里安的StreamToFile作家,發現here

  • 在您的應用程序添加的Global.asax.cs中:

public static void RegisterCustomViewEngines(ViewEngineCollection viewEngines) 
{ 
    //viewEngines.Clear(); //This seemed like a bad idea to me. 
    viewEngines.Add(new EmbeddedResourceViewEngine()); 
} 

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    RegisterRoutes(RouteTable.Routes); 
    RegisterCustomViewEngines(ViewEngines.Engines); 
} 
+0

在我的情況下,resStream爲null。支持ReflectionPermissions的一些問題(http://msdn.microsoft.com/en-us/library/xc4235zt(v=vs.110).aspx)。有沒有人有同樣的問題? – 2014-07-27 13:24:32

3

短短几年增加的卡森赫裏克出色的帖子...

  1. 您將需要解決幾個引用(你將需要包括System.Runtime.Remoting到您的項目)。

  2. Utils.SaveStreamToFile需要改變,以 - >

    System.Runtime.Remoting.MetadataServices.MetaData.SaveStreamToFile(resStream, rootPath + fileName + "." + extension); 
    
  3. 您可能會收到錯誤 - 視圖必須從WebViewPage,或WebViewPage<TModel>派生。答案在這裏:The view must derive from WebViewPage, or WebViewPage<TModel>

  4. 當您部署項目時,很有可能在加載項目時出現錯誤。您需要爲APP POOL提供您正在使用(完整)權限的文件夾。

相關問題