在ASP classic中获取Server.Executed文件的名称

时间:2012-11-09 16:39:25

标签: asp-classic vbscript

我有一个全局include.asp文件,其中包含以下代码:

if SomeCondition then
    Response.Clear
    Response.Status = "404 Not Found"
    Server.Execute "/error404.asp"
    Respnse.End
end if

另外两个文件content.asperror404.asp #include此文件。

内容文件将 SomeCondition 设置为true,从而导致错误页面 Server.Execute 。但是,错误页面内的相同条件也是如此。这会产生无限循环,最终会出现以下错误:

Server object error 'ASP 0227 : 80004005'
Server.Execute Failed
/include.asp, line 1111
The call to Server.Execute failed

如何避免无限循环?我记得这些解决方法:

A

if SomeCondition then
    if GetExecutedFileNameSomehow() <> "error404.asp" then
       ' ...
    end if
end if

但我似乎无法通过代码获取错误文件的名称(我查看了内部服务器变量,所有点变量都指向调用文件,即内容)。

使用共享变量,例如在内容文件中设置 BeingExecuted = true 并在错误文件中检查它,但Server.Execute的问题是执行的脚本无法访问调用文件的变量。

请建议。

2 个答案:

答案 0 :(得分:2)

你是对的,由于Server.Execute的性质,被调用的脚本不知道它的真正起源,我找不到任何找到它的方法。

那就是说,我担心你不得不求助于丑陋的工作,我认为最可靠的是使用Session变量作为“共享变量”。

include.asp 中有这样的代码:

strFileToExecute = "/error404.asp"
If (Session("currently_executing")<>strFileToExecute) And (SomeCondition) Then
    Response.Clear
    Response.Status = "404 Not Found"
    Session("currently_executing") = strFileToExecute
    Server.Execute strFileToExecute
    Session("currently_executing") = ""
    Response.End
End If

逻辑是在调用Execute方法之前设置Session变量。这样当执行 error404.asp 并再次包含相同的代码时,将设置Session变量的值,并且您知道中止操作,从而避免无限循环。

答案 1 :(得分:0)

如果你真的必须在404.asp中包含include.asp,我希望你对“Somecondition”的测试在sub或function中。你可以在include.asp中定义一个变量,在404.asp中将其设置为true并在你的条件下测试它。

因此:

内部include.asp

Dim blnNot404page : blnNot404page = true

在404.asp内部(在包含include.asp之后,在代码中偏离课程)

blnNot404page = false

再次在include.asp内部

if (SomeCondition and blnNot404page) then
    Response.Clear 
    Response.Status = "404 Not Found"
    Server.Execute "/error404.asp"
    Response.End
end if
相关问题