使用VirtualPathProvider动态加载视图时的引用问题

发布于 2024-08-08 20:05:03 字数 2124 浏览 8 评论 0原文

我有以下一组类,用于在视图中动态加载。下面的代码在使用 .RenderPartial 调用时效果很好。

public class VirtFile:VirtualFile
{
    public VirtFile(string virtualPath) : base(virtualPath)
    {
    }

    public override Stream Open()
    {

        string path = this.VirtualPath;
        Stream str = new MemoryStream();
        StreamWriter writer = new StreamWriter(str);
        writer.Write(@"<%@ Control Language=""C#"" Inherits=""System.Web.Mvc.ViewUserControl"" %>
            <%="Test"%> 

            ");
        writer.Flush();
        str.Position = 0;
        return str;
    }
}
public class Provider:VirtualPathProvider
{
    public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return null;
        var dependency = new System.Web.Caching.CacheDependency(virtualPath);
        return dependency;// base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
    }
    public override bool DirectoryExists(string virtualDir)
    {
        if (IsVirtual(virtualDir))
        {
            return true;
        }
        return base.DirectoryExists(virtualDir);
    }
    public override bool FileExists(string virtualPath)
    {
        if (IsVirtual(virtualPath))
        {
            return true;
        }
        return base.FileExists(virtualPath);
    }
    public override VirtualFile GetFile(string virtualPath)
    {

        if(IsVirtual(virtualPath))
        {
            return new VirtFile(virtualPath);                
        }

        return base.GetFile(virtualPath);
    }

    private bool IsVirtual(string virtualPath)
    {
        return virtualPath.Contains("Database");
    }

但是,如果我尝试将 <%="Test"%> 更改为 <%=new Model.Category()%>,则创建一个类型化的查看我收到一条错误消息,指出“找不到类型或命名空间名称“模型”(是否缺少 using 指令或程序集引用?)”。但是,如果将相同的代码简单地放置在 .ascx 文件中,则该代码也可以工作。

我是否遗漏了一些东西,似乎流是否来自文件系统或自定义 VirtualPathProvider 它应该具有相同的加载程序集,因为 <%=AppDomain.CurrentDomain.ApplicationIdentity%> 返回来自文件系统或自定义提供程序的相同值。

I have the following set of classes that I used to dynamically load in a View. The code below works well when called with .RenderPartial.

public class VirtFile:VirtualFile
{
    public VirtFile(string virtualPath) : base(virtualPath)
    {
    }

    public override Stream Open()
    {

        string path = this.VirtualPath;
        Stream str = new MemoryStream();
        StreamWriter writer = new StreamWriter(str);
        writer.Write(@"<%@ Control Language=""C#"" Inherits=""System.Web.Mvc.ViewUserControl"" %>
            <%="Test"%> 

            ");
        writer.Flush();
        str.Position = 0;
        return str;
    }
}
public class Provider:VirtualPathProvider
{
    public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return null;
        var dependency = new System.Web.Caching.CacheDependency(virtualPath);
        return dependency;// base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
    }
    public override bool DirectoryExists(string virtualDir)
    {
        if (IsVirtual(virtualDir))
        {
            return true;
        }
        return base.DirectoryExists(virtualDir);
    }
    public override bool FileExists(string virtualPath)
    {
        if (IsVirtual(virtualPath))
        {
            return true;
        }
        return base.FileExists(virtualPath);
    }
    public override VirtualFile GetFile(string virtualPath)
    {

        if(IsVirtual(virtualPath))
        {
            return new VirtFile(virtualPath);                
        }

        return base.GetFile(virtualPath);
    }

    private bool IsVirtual(string virtualPath)
    {
        return virtualPath.Contains("Database");
    }

But if I try to change the <%="Test"%> to <%=new Model.Category()%>, of create a typed View I get an error stating that "The type or namespace name 'Model' could not be found (are you missing a using directive or an assembly reference?)". However, the same code works if it is simply placed in an .ascx file.

Am I missing something, it seems like wether the stream comes from the file system or a custom VirtualPathProvider it should have the same loaded assemblies, since <%=AppDomain.CurrentDomain.ApplicationIdentity%> returns the same value from either the file system or the custom provider.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最近可好 2024-08-15 20:05:03

尝试添加

<%@ Import Namespace="MyApp.Model" %>

到动态用户控制字符串。

编辑:

当然,您也可以使用类型的完全限定名称,将 Model.Category() 更改为 MyApp.Model.Category().大多数时候,我导入名称空间。只是风格偏好。

Try adding

<%@ Import Namespace="MyApp.Model" %>

to your dynamic user control string.

EDIT:

Of course, you could also use the fully qualified name for the type, changing Model.Category() to MyApp.Model.Category(). Most of the time, I import the namespace. Just a style preference.

莫言歌 2024-08-15 20:05:03

你的 Model 类是什么样的?它是否包含在某个名称空间中? VPP 是非常神奇的东西,可以发挥很多魔力,只需确保当您将“字符串”与虚拟 asp.net“页面”内容一起传递时,您提供了类的完整路径,这样更安全。或者,另一种选择是使用 web.config 链接您的命名空间,以便应用程序找到您的类。

How your Model class look like? Is it wrapped within some namespace? VPP is pretty amazing thing and can do a lot of magic, just make sure when you passing 'string' with your virtual asp.net 'page' content you provide full path to you classes, it's safer this way. or, another option, use your web.config to link your namespaces, so app will find your classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文