ATL_DEV
ATL_DEV

Reputation: 9591

Cryptic error message from VirtualPathProvider

I have several ASCX files scattered throughout a ASP.NET legacy project. I want to display my ascx files instead which has different markups but still references the old codebehind types.

Right now I am getting a pesky error message:

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'.

What could be the cause of this error? My Open method isn't being called either. What gives.

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

}

Upvotes: 2

Views: 338

Answers (1)

Peter Rang
Peter Rang

Reputation: 42

Because I stumbled upon this and for the sake of completeness:

This is a known bug caused by a compilation error of the loaded file. This error is somehow substituted with a cryptic message So In most cases nothing seems to be wrong with the virtual path provider.

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

Upvotes: 1

Related Questions