3

我只是在查看一些旧代码并发现以下内容(在 foo.asp 中):

Const ASP_FILENAME = "foo.asp"  ' TODO: Update this to the name of this file (if changed)

该变量仅用于记录错误。(即“foo.asp 中的错误 - 无法创建 xxxxx 对象。”)有没有办法避免这种情况?

谢谢!

4

4 回答 4

4

您可以解析Request.ServerVariables("url")以获取文件名部分。谷歌搜索发现了这段代码,我不认为它使用了 SCRIPT_NAME 服务器变量,这似乎确实更有意义,还考虑了可能存在的任何 url 重写:

function getFileName(fpath, returnExtension)
        tmp = fpath
        if instrRev(tmp,"/") > 0 then
              tmp = mid(tmp, instrRev(tmp,"/")+1)
        end if
        if returnExtension = false then
              if instrRev(tmp,".") > 0 then
                    tmp = left(tmp, instrRev(tmp,".")-1)
              end if
        end if
        getFileName = tmp
  end function

  filename = request.ServerVariables("SCRIPT_NAME")
  Const ASP_FILENAME = getFileName(filename, true)
于 2009-01-16T14:41:24.543 回答
3

来自现已解散的 Aspfaq.com(感谢Archive.org):

如何获取当前 URL/页面的名称?

这个很简单,但是有两个部分。

要检索当前文件的名称,您可以使用以下任何一种:

<% 
    Response.Write Request.ServerVariables("SCRIPT_NAME") & "<br>" 
    Response.Write Request.ServerVariables("PATH_INFO") & "<br>" 
    Response.Write Request.ServerVariables("URL") & "<br>" 
%>

要使该路径成为本地路径(例如,与 FileSystemObject 一起使用),只需将 server.mappath() 方法应用于结果。

要获取整个 URL,包括 http:// 或 https:// 前缀,您可以这样做:

<% 
    prot = "http" 
    https = lcase(request.ServerVariables("HTTPS")) 
    if https <> "off" then prot = "https" 
    domainname = Request.ServerVariables("SERVER_NAME") 
    filename = Request.ServerVariables("SCRIPT_NAME") 
    querystring = Request.ServerVariables("QUERY_STRING") 
    response.write prot & "://" & domainname & filename & "?" & querystring 
%>

要仅获取页面名称,请使用以下内容:

<% 
    scr = Request.ServerVariables("SCRIPT_NAME") & "<br>" 
    if instr(scr,"/")>0 then 
        scr = right(scr, len(scr) - instrRev(scr,"/")) 
    end if 
    response.write scr 
%>

或者,没有 IF 逻辑:

<% 
    scr = Request.ServerVariables("SCRIPT_NAME") & "<br>" 
    loc = instrRev(scr,"/") 
    scr = mid(scr, loc+1, len(scr) - loc) 
    response.write scr 
%>

现在。如果您的文件是另一个文件中的#INCLUDE,则上述脚本将生成 CALLING 文件的名称(由于包含的文件首先集成到调用脚本中,因此其中的 ASP 全部在“父”的上下文中执行' 文件)。解决此问题的一种方法是在加载每个包含文件之前重新填充 current_filename 变量,例如:

<% 
      current_filename = "filetoinclude.asp" 
 %> 

<!--#include file='filetoinclude.asp'-->

(不,不要尝试将 current_filename 作为变量传递给 #INCLUDE 指令;请参阅文章 #2042。)

然后,在 filetoinclude.asp 中:

<% 
    Response.Write "Current file: " & current_filename 
%>

当然,您也可以轻松地在每个包含文件中硬编码文件名。但是我认为该解决方案会在某种程度上破坏至少在某种程度上动态地检索该信息的目的。

于 2009-01-16T14:45:45.767 回答
0

我不知道 server.mappath 是否存在于传统的 asp 中,但如果存在,您可以使用 if 来了解页面文件名。

于 2009-01-16T14:42:46.647 回答
0

并不是说这里的任何人 [在此处插入离散的清嗓子] 仍然使用 Classic ASP 来维护和支持遗留应用程序,但我最近需要做一些类似的事情。拒绝接受“经典 ASP 不可能”的回应,我开始寻找方法并提出以下解决方案。

这种方法基本上利用底层操作系统命令来逐字确定当前文件名(其结果等同于使用__FILE__PHP 中的魔术常量),无论它是文件包含(*.inc)还是脚本本身(*.asp)。

首先,为了支持一些清理(如果你愿意,你可以用其他一些更“最佳”的方式来做到这一点):

'Regex helpers
Function NewRegex(ByVal pattern, ByVal ignore_case, ByVal global)
  Set NewRegex = New RegExp
  NewRegex.Pattern = pattern
  NewRegex.IgnoreCase = ignore_case
  NewRegex.Global = global
End Function

Function RegexMatch(ByVal pattern, ByVal subject)
  RegexMatch = RegexMatches(subject, pattern, True, False)
End Function

Function RegexMatches(ByVal subject, ByVal pattern, ByVal ignore_case, ByVal global)
  RegexMatches = NewRegex(pattern, ignore_case, global).Test(subject)
End Function

现在是“反思”的时间:

Function GetCurrentFilename(ByVal uniqueId)
  '1. Enforce uniqueId format
  If Not RegexMatch("^[0-9a-f]+$", uniqueId) Then
    Exit Function
  End If

  '2. Use findstr to scan "readable" files in current directory for uniqueId
  Dim shell, cmd, process, fs, filename
  Set shell = Server.CreateObject("WScript.Shell")

  'See findstr /? for details on switches used below
  'cmd = C:\Windows\system32\cmd.exe /c findstr /P /M /C:"uniqueId" "C:\Inetpub\wwwroot\includes\*"
  cmd = shell.ExpandEnvironmentStrings("%COMSPEC%") & " /c findstr /P /M /C:""" & uniqueId & """ """ & Server.MapPath(".") & "\*"""
  Set process = shell.Exec(cmd)

  '3. Use Scripting.FileSystemObject to return the filename portion of the first result returned
  Set fs = Server.CreateObject("Scripting.FileSystemObject")
  GetCurrentFilename = fs.GetFileName(process.StdOut.ReadLine())

  Set fs = Nothing
  Set process = Nothing
  Set shell = Nothing
End Function

然后,在您想要“检查”当前文件名的任何文件中,只需删除以下行,传入一些唯一标识符,该标识符不应存在于当前目录中的任何其他文件中,但这个文件:

'myfile.inc
Response.Write "This is in " & GetCurrentFilename("908ab098c")

结果:

This is in somefile.inc

哦,如果你对我需要用它来做什么感兴趣,下面是它在一个简单的断点函数中使用的样子:

Function Breakpoint(ByVal line_no, ByVal uniqueId, ByVal msg)
  Dim fn
  fn = GetCurrentFilename(uniqueId)

  Response.Write "[!] Breakpoint hit at Line " & CLng(line_no) & " in " & fn & ": " & Server.HtmlEncode(msg) & vbNewLine
  Response.End
End Function

这样在我的代码的第 20 行,如果我想添加自己的断点,它看起来像这样:

Breakpoint 20, "B0001", "Some debug output here"

输出:

[!] Breakpoint hit at Line 20 in somefile.inc: Some debug output here

快乐编码!

于 2015-09-21T19:27:18.513 回答