经典 ASP Server.MapPath() 在 global.asa 中无法按预期工作
在经典 ASP 中,Server.MapPath() 在 global.asa 内的 Application_OnStart 事件中并不总是正常工作。 我在虚拟根目录中的 "\testfolder\test.asp" 处有一个 ASP 页,在 "\xsl\transform.xsl" 处有一个 XSLT 文件。 我的虚拟根目录位于“c:\inetpub\wwwroot\testapp\”。
我在 ASP 页面中使用 MapPath 来获取 XSLT 文件的完整路径。 调用是:
sXslPath = Server.MapPath("xsl\transform.xsl")
有时 MapPath 按预期返回 "c:\inetpub\wwwroot\testapp\xsl\transform.xsl",其他时候则错误地返回 "c:\inetpub\wwwroot\ testapp\testfolder\xsl\transform.xsl"。 错误的路径显然会导致严重的问题。
In Classic ASP, Server.MapPath() doesn't always work properly in the Application_OnStart event within global.asa. I have an ASP page at "\testfolder\test.asp" within a virtual root, I have an XSLT file at "\xsl\transform.xsl". My virtual root is located in "c:\inetpub\wwwroot\testapp\".
I use MapPath within the ASP page to get the full path to the XSLT file. The call is:
sXslPath = Server.MapPath("xsl\transform.xsl")
Some times MapPath returns "c:\inetpub\wwwroot\testapp\xsl\transform.xsl" as expected, other times it incorrectly returns "c:\inetpub\wwwroot\testapp\testfolder\xsl\transform.xsl". The incorrect path obviously causes serious problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这里回答我自己的问题:
出现此问题是因为在Application_OnStart中调用时,MapPath错误地包含了导致应用程序的页面上下文启动。 如果应用程序尚未启动时要运行的第一个 ASP 页面不在虚拟根的根目录中,则 MapPath 会感到困惑,并将调用的 ASP 页面的路径添加到它返回的路径中。
例如,如果启动应用程序的页面位于 "c:\inetpub\wwwroot\testapp\folder1\folder2\test.asp" 中,则 MapPath 会错误地添加 "\folder1\folder2 " 进入路径中间并返回 "c:\inetpub\wwwroot\testapp\folder1\folder2\xsl\transform.xsl"
如果您的网站仅在根文件夹中包含文件或者不在 global.asa 中使用 MapPath 那么你永远不会注意到这个小奇怪的地方。 我怀疑有很多 ASP Classic 站点有时会因此而无法正常启动,但它们的所有者只是快速进行 iisreset,而不知道到底出了什么问题。
这样做的结果是,如果您的网站在除根文件夹之外的任何位置都包含 ASP 文件,则您无法可靠地使用 global.asa 中的 MapPath。
如果它是一次性网站,那么最简单的解决方案就是对 global.asa 中使用的任何路径进行硬编码。
如果您基于 ASP Classic 向其他人销售产品,则不能选择对路径进行硬编码。 您要么必须将 MapPath 的所有使用移出应用程序启动,要么通过将路径作为安装程序的一部分写入 ASP 文件来解决该问题。
I am answering my own question here:
This problem occurs because when called in Application_OnStart, MapPath incorrectly includes the context of the page that caused the application to startup. If the first ASP page to be run when the application isn't yet started is not in the root of the virtual root then MapPath gets confused and adds the path to the called ASP page to the path it returns.
So for example if the page that started the app was in "c:\inetpub\wwwroot\testapp\folder1\folder2\test.asp" then MapPath would incorrectly add "\folder1\folder2" into the middle of the path and return "c:\inetpub\wwwroot\testapp\folder1\folder2\xsl\transform.xsl"
If your website only has files in the root folder or doesn't use MapPath in global.asa then you will never notice this little oddity. I suspect there are lots of ASP Classic sites out there that fail to startup properly sometimes because of this, but their owners just do a quick iisreset, not knowing what quite went wrong.
The result of this is that you can't reliably use MapPath in global.asa if you have a website that has ASP files anywhere other than just the root folder.
If it is a one-off website then the easiest solution is to just hard code any paths you use in global.asa.
If you sell a product to other people based on ASP Classic then hard coding the paths is not an option. You either have to move all usage of MapPath out of the application startup or deal with the issue by writing paths into your ASP files as part of the installer.
或者使用
sXslPath = Server.MapPath("\xsl\transform.xsl")
那么它将自然地从根目录映射路径
如果您在不是服务器的操作系统中的 iis 上进行开发, ,root 将是默认网站,您将必须记住在部署时进行更改...
alternatively use
sXslPath = Server.MapPath("\xsl\transform.xsl")
which will then map the path from the root directory
naturally if you are developing on iis in a OS that is not a server, root will be the default website, you would have to remember to change on deployment...