2013-01-31 46 views
0

我有兩個使用相同視圖的asp.net MVC 3 web項目,我想將這些視圖放在一個包含常見項目的Web項目中控制器,actionfilters,擴展方法等等(這兩個項目上方的項目文件夾)。Asp.net定製剃鬚刀視圖引擎開箱即用訪問視圖

我創建了這個自定義視圖引擎,修改視圖和局部視圖,位置。

public class GenericViewEngine : RazorViewEngine 
    { 
     public GenericViewEngine(): this(null) 
     { } 
     public GenericViewEngine(IViewPageActivator viewPageActivator) 
     { 
      base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/../Framework.Web/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml", "~/../Framework.Web/Views/Shared/{0}.cshtml" }; 
      base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/../Framework.Web/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml", "~/../Framework.Web/Views/Shared/{0}.cshtml" }; 
      base.PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/../Framework.Web/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml", "~/../Framework.Web/Views/Shared/{0}.cshtml" }; 
      base.FileExtensions = new string[] { "cshtml" }; 
     } 
     protected override IView CreatePartialView (ControllerContext controllerContext, string partialPath) 
     { 
      return base.CreatePartialView(controllerContext, partialPath); 
     } 
     protected override IView CreateView (ControllerContext controllerContext, string viewPath, string masterPath) 
     { 
      return base.CreateView(controllerContext, viewPath, masterPath); 

     } 
    } 

我得到一個異常,說:不能使用領先..退出頂部目錄以上。

我怎麼能把這兩個項目的共同意見從每個web項目中拿出來?有沒有解決方案可以避免將視圖編譯成主要的通用Web項目?

在此先感謝。

何塞。

回答

0

是否有避免將視圖編譯成主要通用Web項目的解決方案?

是的,你可以將它們嵌入的資源投入到一些插件,組裝,並在多個項目中重用他們。爲此,您必須編寫一個自定義的VirtualPathProvider,它能夠從當前應用程序之外的非標準位置加載視圖。你也可以看看RazorGenerator project。這裏有一個accompanying blog post解釋一些必要的步驟。基本上你需要安裝Razor Generator Extension,它將預編譯你的Razor視圖作爲你可以在你的MVC應用程序中引用的類庫的一部分。

相關問題