我有几个ASCX文件分散在一个 ASP.NET 遗留项目中。我想显示我的 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);
}
}
}
因为我偶然发现了这个,为了完整起见:
这是由加载文件的编译错误引起的已知错误。此错误以某种方式替换为神秘消息因此,在大多数情况下,虚拟路径提供程序似乎没有任何问题。
参考:https://freedrum99.wordpress.com/2007/11/28/the-virtualpathprovider-returned-a-virtualfile-object-with-virtualpath-set-to/