asp-classic

How can I get the full url including characters after a # char


I have the following code:

<%@Language="VBSCRIPT"%>
<html>
<head>
<title>in/Test Page 2</title>
</head>
<body>
<% 
response.write "Request.QueryString = """ & Request.QueryString & """<br />"

for each item in request.QueryString
    response.write (item + " = " + Request.QueryString(item) + "<br>") 
next
%>
</body>
</html>

When I call it with this url:

TestPage2.asp?id=1&turl=http://www.google.com?id=1&url=generic#index

Which produces this output

Request.QueryString = "id=1&turl=http://www.google.com?id=1&url=generic"
id = 1
turl = http://www.google.com?id=1url=generic

How do I get the bit after the # char in the original url? I've looked all through the Request.Servervariable's.


Solution

  • Short answer: You can't


    It's by design.

    From RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax - Section 4.1 Fragment Identifier
    When a URI reference is used to perform a retrieval action on the identified resource, the optional fragment identifier, separated from the URI by a crosshatch (“#”) character, consists of additional reference information to be interpreted by the user agent after the retrieval action has been successfully completed. As such, it is not part of a URI, but is often used in conjunction with a URI.

    Basically the # is never sent to the server so you can't retrieve it from Classic ASP. The client (Internet Browser) removes the # fragment from the URI before it is sent.

    Having said that it is possible to retrieve it from the client-side using JavaScript then pass the value via a form / querystring to the server where Classic ASP can retrieve and use it.

    However, if you control the URL the simplest solution is to encode the crosshatch (#) as part of the querystring value using URL encoding. Doing this will encode the crosshatch %23 instead of # resulting in it not being treated as a Fragment Identifier by the internet browser.