可插入的MVC视图返回null

时间:2012-04-05 16:17:16

标签: c# asp.net .net asp.net-mvc razor

我尝试了一些来自herehere的代码,但是在尝试引用位​​于单独项目中的远程视图时,我在页面上收到错误:“值不能为null 。参数名称:stream“

下面是我的Master MVC 4项目中的AssemblyResourceProvider和controller。我还附上了整个解决方案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

1 个答案:

答案 0 :(得分: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;
    }
}

真正唯一的区别是我已经做到了,所以你可以在那里坚持断点并走路看看它发现了什么。把你的断点放在并走过代码,汇编== null?如果没有,resourceStream是否为null?