简体   繁体   English

检查预编译的asp.net网站中是否存在ascx控件

[英]Check if ascx control exist in precompiled asp.net website

I would like to check if an ASCX file exists before I open it because it is loadded dynamically. 我想在打开ASCX文件之前检查它是否存在,因为它是动态加载的。 That should be easy by using the following code: System.IO.File.Exists(Server.MapPath(controlPath)) . 使用以下代码应该很容易: System.IO.File.Exists(Server.MapPath(controlPath)) But this doesn't work in precompiled website because ASCX files don't exist anymore. 但这在预编译的网站中不起作用,因为ASCX文件不再存在。

Any help will be appreciated. 任何帮助将不胜感激。

Update: I precompile a website without "Allow precompiled site to be updatable" option. 更新:我没有“允许预编译的站点可更新”选项的情况下预编译了一个网站。

File.Exists will not work because when an .ascx is precompiled it will not be physically present on the disk. File.Exists无法正常工作,因为预编译.ascx时,它实际上不会出现在磁盘上。

HostingEnvironment.VirtualPathProvider.FileExists will not work as well, the default implementation is MapPathBasedVirtualPathProvider, which expects files to be physically present on the disk. HostingEnvironment.VirtualPathProvider.FileExists不能正常运行,默认实现是MapPathBasedVirtualPathProvider,它期望文件实际存在于磁盘上。

In such scenario, TemplateControl.LoadControl use the BuildManager which already cached those virtual files, so their physical presence is not checked. 在这种情况下,TemplateControl.LoadControl使用已经缓存了这些虚拟文件的BuildManager,因此不会检查其物理状态。

Using reflection, it's possible to check if these files are cached (read: exist): 使用反射,可以检查这些文件是否已缓存(读取:存在):

public static bool IsPrecompiledFileExists(string virtualPath)
{
    Type virtualPathType = BuildManager.GetType("System.Web.VirtualPath", true);
    MethodInfo createMethodInfo = virtualPathType.GetMethod("Create", new Type[] {typeof(string)});
    object virtualPathObject = createMethodInfo.Invoke(null, new object[] { virtualPath });

    MethodInfo getVPathBuildResultFromCacheMethodInfo = typeof(BuildManager).GetMethod("GetVPathBuildResultFromCache", BindingFlags.Static | BindingFlags.NonPublic);
    object buildResult = getVPathBuildResultFromCacheMethodInfo.Invoke(null, new object[] { virtualPathObject });
    return buildResult != null;
}

To make the solution compatible with additional deployment scenarios, you may probably want to use: 为了使该解决方案与其他部署方案兼容,您可能需要使用:

public static bool IsVirtualFileExists(string virtualPath)
{
    return HostingEnvironment.VirtualPathProvider.FileExists(virtualPath) ||
           IsPrecompiledFileExists(virtualPath);
}

Edit: Peter Blum has a better and faster solution. 编辑: Peter Blum有一个更好,更快的解决方案。

ps For .Net Framework 2.0, BuildManager.CreateInstanceFromVirtualPath could be used as well (We can learn if the file exist or not based on the exception thrown). ps对于.Net Framework 2.0,也可以使用BuildManager.CreateInstanceFromVirtualPath(我们可以根据引发的异常来了解文件是否存在)。

Some times you need to check if a user control exists before dynamically load it to a page. 有时,您需要先检查用户控件是否存在,然后才能将其动态加载到页面上。 Here are two ways to accomplish this: 这有两种方法可以实现:

using System.Web.Hosting;
...

string virtualPath = "MyControl.ascx";
// or: "controls/MyControl.ascx"
// or: "../controls/MyControl.ascx"
// or: "~/controls/MyControl.ascx"

if (HostingEnvironment.VirtualPathProvider.FileExists(virtualPath))
{
Control control = LoadControl(virtualPath);
...
}

using System.IO;
...

string virtualPath = "MyControl.ascx";
// or: "controls/MyControl.ascx"
// or: "../controls/MyControl.ascx"
// or: "~/controls/MyControl.ascx"

if (File.Exists(Server.MapPath(virtualPath)))
{
Control control = LoadControl(virtualPath);
 ...
}
 string ucName = "MyUC.ascx";
 if(File.Exists(Server.MapPath("/NameProject/" + (ucName))))
      controlToLoad = ucName;
LoadControl(controlToLoad);

Worked for me =)) 为我工作=))

System.IO.File.Exists(Server.MapPath(path)) should do the trick. System.IO.File.Exists(Server.MapPath(path))应该可以解决问题。 As @Leo said in your comment. 正如@Leo在您的评论中所说。 ascx is a custom user control that is used in aspx page and they are still on the same location when you deployed your project. ascx是一个自定义用户控件,在aspx页面中使用,并且在部署项目时它们仍位于同一位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM