how to get the remote url - asp.net

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))

Related

Callback URL is ignoring parameter value after &

I got stuck at this point. After login i am getting referral-url which i am putting into www.url.com?par1=val&callback="referral-url". My referral url is like www.ref-url.com?param1=val1&param2=val2&param3=val3. My problem is that i am getting a cut url i.e., www.ref-url.com?param1=val1 after login. I think it is ignoring url after '&'.I am using classic asp for development. Any Help would be very helpful.
You need to use Server.URLEncode if you're including a URL as a querystring parameter, especially if the included URL also contains querystrings.
Dim login_redirect, login_referrer
login_redirect = "http://www.url.com/?par1=val&callback="
login_referrer = "http://www.ref-url.com/?param1=val1&param2=val2&param3=val3"
response.write login_redirect & Server.URLEncode(login_referrer)
Output:
http://www.url.com/?par1=val&callback=http%3A%2F%2Fwww%2Eref%2Durl%2Ecom%2F%3Fparam1%3Dval1%26param2%3Dval2%26param3%3Dval3
Passing the URL with query inside another URL query is a bit tricky. The only way it works is to encode it. For example:
https://website.com/?a=1&url=https%3A%2F%2Fwebsite.com%2F%3Fz%3D1%26y%3D2
But, when you want to return to the url you passed through query, you need to decode it otherwise it will not work. You can use the following function on your "login Page" before redirecting the url.
Function URLDecode(sConvert)
Dim aSplit
Dim sOutput
Dim I
If IsNull(sConvert) Then
URLDecode = ""
Exit Function
End If
' convert all pluses to spaces
sOutput = REPLACE(sConvert, "+", " ")
' next convert %hexdigits to the character
aSplit = Split(sOutput, "%")
If IsArray(aSplit) Then
sOutput = aSplit(0)
For I = 0 to UBound(aSplit) - 1
sOutput = sOutput & _
Chr("&H" & Left(aSplit(i + 1), 2)) &_
Right(aSplit(i + 1), Len(aSplit(i + 1)) - 2)
Next
End If
URLDecode = sOutput
End Function
For example, you should have above function and the following code on your login page:
Dim callback
callback = Request("callback")
callback = URLDecode(callback)
Response.redirect(callback)

get current url of the page (used URL Rewrite)

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)

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.

VB.net / asp.net: Get URL without filename

i want to set a link in VB.net dynamically to a file.
My url looks like that:
http://server/folder/folder2/file.aspx?get=param
I tried to use Request.URL but i have not found any solution to get only
http://server/folder/folder2/
without the query string and without the filename.
Please help.
Dim url = Request.Url;
Dim result = String.Format(
"{0}{1}",
url.GetLeftPart(UriPartial.Authority),
String.Join(string.Empty, url.Segments.Take(url.Segments.Length - 1))
)
You can easily get a relative file path using the Request instance, then work with that, using Path class ought to help:
Dim relativePath = Request.AppRelativeCurrentExecutionFilePath
Dim relativeDirectoryPath = System.IO.Path.GetDirectoryName(relativePath)
It's worth noting that GetDirectoryName might transform your slashes, so you could expand the path:
Dim mappedPath = HttpContext.Current.Server.MapPath(newpath)
So, to remove redundancy, we could shorten this:
Dim path = _
Server.MapPath( _
Path.GetDirectoryName( _
Request.AppRelativeCurrentExecutionFilePath)))
But you'll need to check for possible exceptions.
You can use Uri.Host to get the computer name and then Uri.Segments (an array) to get everything up to the filename, for example:
var fileName = Uri.Host && Uri.Segments(0) && Uri.Segments(1)
This will give you: server/folder/folder2
If you have a variable number of segments, you can iterate over them and ignore the last one.
I hope that might help :)

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
}

Resources