2012-04-05 110 views
0

我嘗試了一些herehere的代碼,但在嘗試引用位​​於單獨項目中的遠程視圖時,頁面上出現錯誤:「值不能爲空。 :流「可插入的MVC視圖返回null

以下是我的主MVC 4項目中的AssemblyResourceProvider和控制器。我也附上了整個解決方案本身here。任何人都可以幫忙看看發生了什麼問題嗎?感謝您的任何幫助或建議。

public class AssemblyResourceProvider : System.Web.Hosting.VirtualPathProvider 
    { 
     private bool IsAppResourcePath(string virtualPath) 
     { 
      string checkPath = VirtualPathUtility.ToAppRelative(virtualPath); 
      return checkPath.StartsWith("~/Plugin/", StringComparison.InvariantCultureIgnoreCase); 
     } 

     public override bool FileExists(string virtualPath) 
     { 
      return IsAppResourcePath(virtualPath) || base.FileExists(virtualPath); 
     } 

     public override VirtualFile GetFile(string virtualPath) 
     { 
      return IsAppResourcePath(virtualPath) 
       ? new AssemblyResourceVirtualFile(virtualPath) 
       : base.GetFile(virtualPath); 
     } 

     public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) 
     { 
      return !IsAppResourcePath(virtualPath) 
       ? base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart) 
       : null; 
     } 
    } 

    public class AssemblyResourceVirtualFile : VirtualFile 
    { 
     string path; 

     public AssemblyResourceVirtualFile(string virtualPath) : base(virtualPath) 
     { 
      path = VirtualPathUtility.ToAppRelative(virtualPath); 
     } 

     public override Stream Open() 
     { 
      string[] parts = path.Split('/'); 
      string assemblyName = parts[2]; 
      string resourceName = parts[3]; 

      var assembly = Assembly.LoadFile(Path.Combine(HttpRuntime.BinDirectory, assemblyName)); 

      return assembly != null 
       ? assembly.GetManifestResourceStream(resourceName) 
       : null; 
     } 
    } 

而且我家的控制器:

public ActionResult Pluggable() 
{ 
    //ViewBag.Name = name; 
    return View("~/Plugin/PluginView1.dll/PluginView1.Views.Home.Pluggable.cshtml"); 
} 

enter image description here

+0

是的,公共覆蓋Stream Open()無法找到程序集並返回null流,或者GetManifestResourceStream找不到具有該名稱的資源。如果cshtml文件未設置爲嵌入式資源或者您的完整名稱空間錯誤,則可能會發生這種情況。 – 2012-04-05 16:19:34

+0

我確實記得要嵌入資源,並且我相信根據另一篇文章我的命名空間是正確的。 – TruMan1 2012-04-05 16:55:36

+0

您是否記得將DLL複製到Bin文件夾中?或者你從哪裏加載你的插件? – 2012-04-05 17:14:22

回答

0

這是我AssemblyResourceVirtualFile,我在一個項目中使用之前:

public class AssemblyResourceVirtualFile : VirtualFile 
{ 
    string path; 
    public AssemblyResourceVirtualFile(string virtualPath) : base(virtualPath) 
    { 
     path = VirtualPathUtility.ToAppRelative(virtualPath); 
    } 
    public override System.IO.Stream Open() 
    { 
     string[] parts = path.Split('/'); 
     string assemblyName = parts[2]; 
     string resourceName = parts[3]; 

     assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName); 

     System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(assemblyName); 
     if (assembly != null) 
     { 
      Stream resourceStream = assembly.GetManifestResourceStream(resourceName); 
      return resourceStream; 
     } 
     return null; 
    } 
} 

真的,唯一的區別就是我」已經做到了,所以你可以在那裏貼上斷點,然後走路看看它是什麼INDS。把你的斷點放在代碼中,是程序集== null?如果沒有,是resourceStream null?

+0

出於某種原因,它對這個事件感到窒息,我從來沒有這樣稱呼過:〜/ Plugin/PluginView1.dll/_ViewStart.cshtml – TruMan1 2012-04-05 17:34:51

+0

那是因爲Razor的默認行爲是加載ViewStart來加載默認設置(佈局文件,等等) – 2012-04-05 17:49:28