get current url of the page (used URL Rewrite) - asp-classic

I am working on classic asp application.
I have use URL rewrite on some pages.
How can i get current url of the page in classic asp?
Example:
http://www.site.com/page.asp ---> url rewrite in IIS ---> http://www.site.com/home/page
so here i want current url of the page which is http://www.site.com/home/page
Please help me.
Thanks.

There's no fancy one function that does it all.
First you need to get the protocol (if it is not always http):
Dim protocol
Dim domainName
Dim fileName
Dim queryString
Dim url
protocol = "http"
If lcase(request.ServerVariables("HTTPS"))<> "off" Then
protocol = "https"
End If
Now the rest with optional query string:
domainName= Request.ServerVariables("SERVER_NAME")
fileName= Request.ServerVariables("SCRIPT_NAME")
queryString= Request.ServerVariables("QUERY_STRING")
url = protocol & "://" & domainName & fileName
If Len(queryString)<>0 Then
url = url & "?" & queryString
End If
Hope it works for you.

You can try to output all ServerVariables like so:
for each key in Request.Servervariables
Response.Write key & " = " & Request.Servervariables(key) & "<br>"
next
Maybe the URL you seek is already there. We use the Rewrite module and there is a ServerVariable called HTTP_X_ORIGINAL_URL that contains the rewritten URL path, e.g. "/home/page" in your example.
Protocol (HTTPS=ON/OFF) and Server (SERVER_NAME) can also be found in the ServerVariables.

If you use URL Rewrite, the url data can only be retrieved in this way:
Request.ServerVariables("HTTP_X_ORIGINAL_URL")
Example
Dim domainName, urlParam
domainName = Request.ServerVariables("SERVER_NAME")
urlParam = Request.ServerVariables("HTTP_X_ORIGINAL_URL")
response.write(domainName & urlParam)

Related

Render a hyperlink in a email template

I am trying to render a hyperlink in an email template which will be sent to the user and if the user clicks on that link it will direct to a unique url. I have given the coding below,
email.AddMailmerge("RequestUrl", "Feedback Requests")
My problem is the link doesnt resolve correctly and take me to the right url. What am I doing wrong?
It resolves as:
C:\Users\Test\Desktop\localhost/Requests/Requests.aspx?Company_ID=KirprZ17bg5u5Qf1
Make sure you Appsetting contains http:// so instead of just
localhost
it needs to be
http://localhost
Be sure to format your href with quotes. Instead of relying on your app.config's value, which is return the C:\ value, use the .NET methods to get the current domain/URL.
Dim target As String = _
String.Format("<a href='{0}/Requests/Requests.aspx?Company_ID={1}'>Feedback Requests</a>",_
Request.Url.GetLeftPart(UriPartial.Authority),_
objCompany.IDHashed)
Dim strRequestLink As String = System.Configuration.ConfigurationManager.AppSettings("DomainName") & "/Requests/Requests.aspx?Company_ID=" & Me.ID & "&key=" & Me.IDHashed
Dim strRequestUrl As String = "http://" & strRequestLink & ""
I used the above formatting.

how to get the remote url

I am using
Dim url As String = Server.MapPath("MyFiles") & "\" &
System.IO.Path.GetFileName(hpf.FileName)
to get the url of the uploaded file.
The result i get is
D:\2008VS\LTC INDIA\LTCIndia v 1.52\AdminPanel\MyFiles\1.jpg
i want it to be like this http://www.abcd.com/AdminPanel/MyFiles/1.jpg
Kindly let me know if posting the whole code would be of any help.
You could use the ResolveUrl method:
Dim url As String = ResolveUrl("~/MyFiles/" & System.IO.Path.GetFileName(hpf.FileName))

Show if page URL matches string in classic ASP?

I'm trying to create an if statement which shows a line of JavaScript only if the current page URL matches a given string. Here's some pseudo code:
if URL matches ("example.com/sitename/") then
response.Write("<script='file.js'></script>")
end if
The problem is how do I check whether the URL of the current page matches the string?
Many thanks!
Thanks for your responses. I'm trying to make it a little more complicated with an array of possible URL matches. I've created the following asp, but it doesn't work properly. The script is being written on a page where the URL doesn't match. Is there anything that looks wrong? I can't figure it out!
<%
Dim pageURL
pageURL = Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL")
URLArray = Array("example.net/folder1/","example.net/folder2/")
For Each URLsnippet In URLArray
if instr(pageURL, URLsnippet) then
response.Write("<!--BEGIN EXCLUDE--><script type='text/javascript' src='script.js'></script><!--END EXCLUDE-->")
else
end if
Next
%>
It should match URLs matching example.net/folder1 and example.net/folder2, but it's also matching example.net folder3!
try
if Request.ServerVariables("URL") = "/myurl.com/script.asp" then
' do stuff
end if
If you just want the url try this
var url = "<%=Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL")";
if(url == "site.com/script.asp")
{
//Do stuff
}
If you also need the querystring try
var url = "<%=Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & "?" & Request.Querystring%>";
if(url == "site.com/script.asp?var=123")
{
//Do stuff
}

sHow to determine the path of the current web site

I wanted to create a function which would return the path of the current web site. This is what I thought was working while running in the IDE:
Public Shared Function WebsiteAbsoluteBaseUrl() As String
Dim RequestObject As System.Web.HttpRequest = HttpContext.Current.Request
Return "http://" & RequestObject.Url.Host & ":" & _
RequestObject.Url.Port & "/" & _
RequestObject.Url.Segments(1)
End Function
this seem like it should work? Is there a more straight forward way?
I am trying to replace all occurrences of the tag "{WebSiteBaseUrl} in an html email with the absolute web site path.
<A HREF="{WebSiteBaseUrl}Files/filename.pdf/>Some Text</A>
I don't want to have to parse the template and try to make sense of it in order to use MapPath function on a relative path to a file name.
The resulting email will become the html body of an html email, so I need the links to be absolute.
Some Text
I guess what I am seeing in dev, running in the IDE, is that "RequestObject.Url.Segments(1)" returns the name of the virtual folder and that in production it seems to be returning the name of the web page. I guess this probably has to do with how the web site is set up in IIS.
What does this suggest about how the web site is set up in production and how would you suggest I modify the code to yield a correct path after replacing {WebSiteBaseUrl} with the result of the function? This assumes that the file "filename.pdf" is in the relativepath ~/files/filesname.pdf.
You can use Request.Url.AbsoluteUri for this. Server.MapPath(".") will give you physical path.
Also you can use other Request properties which are listed here.
Try this to get the website's application path:
Public shared ReadOnly Property FullyQualifiedApplicationPath() As String
Get
Dim appPath As String = Nothing
Dim context As HttpContext = HttpContext.Current
If Not context Is Nothing Then
'Formatting the fully qualified website url/name
appPath = String.Format("{0}://{1}{2}{3}", _
context.Request.Url.Scheme, _
context.Request.Url.Host, _
IIf(context.Request.Url.Port = 80, String.Empty, ":" & context.Request.Url.Port), _
context.Request.ApplicationPath)
End If
Return appPath
End Get
End Property
or try this (just founded):
HttpContext.Current.Request.UrlInternal

How do I call javascript just before a response redirect

I'm trying to run some java script just before a page redirect but it fails to run.
When I comment out the Response.Redirect all works fine but this goes against the particular requirements. Any ideas on how to implement this functionality?
Dim strscript As String = "<script>alert('hello');</script>"
If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
ClientScript.RegisterStartupScript(Me.GetType(), "clientscript", strscript)
End If
Response.Redirect("http://www.google.com")
Your problem is that the Response.Redirect redirects the response (...) before anything is sent back to the client. So what the client gets is a response from Google rather than from your server.
In order to write some javascript on the page and have it execute before sending the client to Google, you'll need to do your redirect in javascript after the alert.
Dim strscript As String = "<script>alert('hello');window.location.href='http://www.google.com'</script>"
If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
ClientScript.RegisterStartupScript(Me.GetType(), "clientscript", strscript)
End If
The client isn't getting a chance to load. Try redirecting from the client side:
Dim strscript As String = "<script>alert('hello');window.location.href("http://www.google.com");</script>"
If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
ClientScript.RegisterStartupScript(Me.GetType(), "clientscript", strscript)
End If
If you want to execute some javascript before redirecting, you will need to do the redirect in javascript and not in ASP.NET.
Dim strscript As String = "<script>alert('hello'); window.location.href='http://www.google.com';</script>"
If Not ClientScript.IsClientScriptBlockRegistered("clientscript") Then
ClientScript.RegisterStartupScript(Me.GetType(), "clientscript", strscript)
End If
Not Working:
string sScript = "<script language='javascript'>alert(\"" + Alertstr + "\"); alert('Record has been Updated Successfully'); </script>";
ClientScript.RegisterStartupScript(typeof(Page), "alert", sScript);
response.redirect("LandingPage.aspx");
Working:
string sScript = "<script language='javascript'>alert(\"" + Alertstr + "\"); alert('Record has been Updated Successfully'); window.location.href = 'LandingPage.aspx'; </script>";
ClientScript.RegisterStartupScript(typeof(Page), "alert", sScript);
If you use a Response.Redirect it actually sends a 3XX response to the browser which causes it to send a request to the URL in the redirect. It won't actually load/render any data contained with the response (actually I don't think any data is sent). If you want it to redirect after the page loads, you may want to either include a META refresh header that redirects a certain amount of time after load or use javascript do to the redirect at the end of your script.

Resources