VirtualPathProvider的隐秘错误消息

时间:2014-02-17 05:21:44

标签: asp.net virtualpathprovider

我在ASP.NET遗留项目中分散了几个ASCX文件。我希望显示我的ascx文件,它具有不同的标记,但仍引用旧的代码隐藏类型。

现在我收到一条令人讨厌的错误消息:

Line 10: <%@ Register src="~/CMS/Header.ascx" tagname="Header" tagprefix="uc2" %>  Line 11: <div id="Content" runat="server"> class="widget">


The VirtualPathProvider returned a VirtualFile object with VirtualPath set to '/MyLib/BootstrapHeader.ascx' instead of the expected '/CMS/Header.ascx'.

导致此错误的原因是什么?我的Open方法也没有被调用。是什么。

  public class CMSContentVirtualPathProvider : VirtualPathProvider
{

    private const string CMSContentPath = "CMS/";
    private const string CMSControlFolderName = "MyLib/";
    private const string CMSPageFolderName = "Pages/";
    private const string CMSMasterPagesFolderName = "Master/";

    private const string CMSPrefix = "Bootstrap";

    public static void AppInitialize()
    {
        HostingEnvironment.RegisterVirtualPathProvider(new CMSContentVirtualPathProvider());
    }

    public override VirtualFile GetFile(string virtualPath)
    {
        // We've encountered my url
        if (IsVirtualPath(virtualPath))
        {
            return base.GetFile(virtualPath);
        }

        var translatedPath = TranslatePath(virtualPath);

        if (base.FileExists(translatedPath))
            return new CMSContentVirualFile(translatedPath);

        return Previous.GetFile(virtualPath);
    }

    private static string TranslatePath(string virtualPath)
    {
        // We've encountered a regular url
        // translate the URL based on the file type and return it if it exists
        var translatedPath = "~/" + CMSContentPath;

        var fileName = CMSPrefix + VirtualPathUtility.GetFileName(virtualPath);

        if (fileName.EndsWith("ascx", StringComparison.OrdinalIgnoreCase))
        {
            translatedPath = VirtualPathUtility.Combine(translatedPath + CMSControlFolderName, fileName);
        }
        else if (fileName.EndsWith("aspx", StringComparison.OrdinalIgnoreCase))
        {
            translatedPath = VirtualPathUtility.Combine(translatedPath + CMSPageFolderName, fileName);
        }
        else if (fileName.EndsWith("master", StringComparison.OrdinalIgnoreCase))
        {
            translatedPath = VirtualPathUtility.Combine(translatedPath + CMSMasterPagesFolderName, fileName);
        }
        return translatedPath;
    }

    public override bool FileExists(string virtualPath)
    {
        if (IsVirtualPath(virtualPath))
            return true;

        return Previous.FileExists(virtualPath);
    }

    public static bool IsVirtualPath(string virtualPath)
    {
        return virtualPath.Contains(CMSContentPath);
    }
}

public class CMSContentVirualFile : VirtualFile
{
    private string CMSPath = "Testing the test and seeing if it works....";
    public CMSContentVirualFile(string virtualPath)
        : base(virtualPath)
    {
        CMSPath = VirtualPathUtility.ToAppRelative(virtualPath);
    }// open is never called

    public override Stream Open()
    {
        ASCIIEncoding encoding = new ASCIIEncoding();
        return new MemoryStream(encoding.GetBytes(this.CMSPath), false);
    }
}

}

1 个答案:

答案 0 :(得分:1)

因为我偶然发现了这一点并且为了完整起见:

这是由加载文件的编译错误导致的已知错误。 这个错误以某种方式被一个神秘的消息所取代 因此,在大多数情况下,虚拟路径提供程序似乎没有任何问题。

参考: https://freedrum99.wordpress.com/2007/11/28/the-virtualpathprovider-returned-a-virtualfile-object-with-virtualpath-set-to/