可插拔MVC视图返回NULL [英] Pluggable MVC view return null

查看:119
本文介绍了可插拔MVC视图返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试以下一些code从<一个href=\"http://stackoverflow.com/questions/236972/using-virtualpathprovider-to-load-asp-net-mvc-views-from-dlls\">here和这里,但我得到一个页面上的错误,当试图引用位于一个单独的项目远程视图:值不能为空参数名:流

I tried following some of the code from here and here, but I am getting an error on the page when trying to reference the remote view located in a separate project: "Value cannot be null. Parameter name: stream"

下面是我的AssemblyResourceProvider和控制器在我的主人MVC 4项目。我还附上了完整的解决方案本身这里。任何人都可以帮忙看看是什么问题呢?感谢您的任何帮助或建议。

Below is my AssemblyResourceProvider and controller in my master MVC 4 project. I have also attached the whole solution itself here. Can anyone help to see what is going wrong? Thanks for any help or suggestions.

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");
}

推荐答案

下面是我的AssemblyResourceVirtualFile,我在一个项目中使用之前:

Here is my AssemblyResourceVirtualFile that I used in a project before:

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;
    }
}

真是唯一的区别就是我做了,所以你可以在那里坚持破发点,走,看看有什么发现。把你的破发点,并在行走code,是装配== NULL?如果没有,是空resourceStream?

Really the only difference is I've made it so you can stick break points in there and walk to see what it finds. Put your break point in and walk the code, is the assembly == null? If not, is resourceStream null?

这篇关于可插拔MVC视图返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆