Thứ Năm, 3 tháng 5, 2012

Why won't QueryString values work with Server.Execute / Server.Transfer?

The Server.Execute and Server.Transfer commands are fairly useful replacements for #includes. However, as many people have pointed out, if you use a QueryString value, you receive this error:

Server object error 'ASP 0235 : 80004005'
Server.Transfer Error
/.asp, line
Invalid URL form or fully-qualified absolute URL was used. Use relative URLs.

or
Server object error 'ASP 0231 : 80004005'
Server.Execute Error
/.asp, line
Invalid URL form or fully-qualified absolute URL was used. Use relative URLs.

Even when your URL *IS* relative (removing the QueryString value makes the page function properly).

Unfortunately, Microsoft has yet to recognize this officially as a bug. So in the meantime, you must rely on session variables or database entries to retrieve any information you would like to pass to the target page.

However, keep in mind that if you had QueryString values coming in to the calling page, you can access those without difficulty in the target page. So as an example, let's say a.asp calls b.asp, and a.asp was called with a.asp?x=1&y=2:

<%
    ' a.asp
    Server.Execute("b.asp")
%>

<%
    ' b.asp
    Response.Write(Request.QueryString("x"))
    Response.Write("
")
    Response.Write(Request.QueryString("y"))
%>

You will see the results printed in the browser, because b.asp still has access to the ServerVariables context.

If you really need to pass *new* QueryString information into b.asp and you can't change that file or change the call to a.asp to include that new information, you can make a.asp redirect to itself with the new information. A simple logic tree will ensure that you only suffer the hit when the new information is not included.

<%
    ' a.asp
    if Request.QueryString("foo") = "" then
        Response.Redirect "a.asp?foo=bar"
    else
        Server.Execute("b.asp")
    end if
%>

<%
    ' b.asp
    Response.Write(Request.QueryString("foo"))
%>

You could also do this using a new go-between page instead of adding logic to a.asp.

(http://classicasp.aspfaq.com/general/why-won-t-querystring-values-work-with-server-execute/server-transfer.html)

Không có nhận xét nào:

Đăng nhận xét