如何加载从一个类库项目的看法? [英] How to load views from a Class Library project?

查看:143
本文介绍了如何加载从一个类库项目的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个的VirtualPathProvider ,并设置视图作为嵌入资源。

I tried to create a VirtualPathProvider and set the view as an embedded resource.

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);
        var assembly = Assembly.LoadFile(assemblyName);

        if (assembly != null)
        {
            return assembly.GetManifestResourceStream(resourceName);
        }
        return null;
    }
}

public class AssemblyResourceProvider : System.Web.Hosting.VirtualPathProvider
{
    public AssemblyResourceProvider() { }

    private bool IsAppResourcePath(string virtualPath)
    {
        String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
        return checkPath.StartsWith("~/App_Resource/", StringComparison.InvariantCultureIgnoreCase);
    }

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

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

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

我的控制器

return View("~/App_Resource/Blog.DLL/Blog.Views.Blog.Latest.cshtml");

它发现的观点,但我最终得到这个错误:

It does find the view, but I end up getting this error:

... view.cshtml'必须从WebViewPage派生,或WebViewPage<的TModel方式>

... view.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.

在尝试使用,以显示局部视图:

When trying to show the partial view using:

@{ Html.RenderAction("Latest", "Blog"); }


有没有办法解决?


Is there a way to fix it?

还是有一个容易的方式存储在一个DLL的看法?

Or is there an easier way to store views on a DLL?

推荐答案

发生这种情况的原因是因为你现在在职未知地点剃刀视图的标准〜/视图/ web.config中不再适用。所以,你可以把一个 @inherits System.Web.Mvc.WebViewPage 您的自定义视图里面,但它可能是一个相当麻烦。

The reason this happens is because you are now serving the razor views from unknown locations the standard ~/views/web.config no longer applies. So you could put a @inherits System.Web.Mvc.WebViewPage inside your custom views but it could be quite a hassle.

您可以签<一个href=\"http://www.chrisvandesteeg.nl/2010/11/22/embedding-$p$p-compiled-razor-views-in-your-dll/#axzz1Hy1JZrjy\">the下面的文章。

这篇关于如何加载从一个类库项目的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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