2014-02-17 133 views
2

我有幾個分散在ASP.NET傳統項目中的ASCX文件。我想顯示我的ascx文件,而不是具有不同的標記,但仍然引用舊的代碼隱藏類型。來自VirtualPathProvider的隱藏錯誤消息

現在我得到一個討厭的錯誤消息:

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

}

回答