Reputation: 5905
A requirement is to get page url, basically I was in need to get ful URL of directory in which page is.
In case of www.domain.com/page.aspx I need www.domain.com/ and in case of www.domain.com/TestApplication1/Page.aspx I need www.domain.com/TestApplication1/
For this I tried:
Request.Url.DnsSafeHost.ToString()
Another code that I was using before that was building complete URL was:
Dim currenturl = HttpContext.Current.Request.Url
Dim url = String.Format("{0}://{1}{2}", currenturl.Scheme, currenturl.Host, VirtualPathUtility.ToAbsolute("~" + Response.RedirectLocation))
BUT there was an issue, they do not work fine if page is inside an application.
For example, they work fine for www.domain.com/Page.aspx but do not work fine if page address is www.domain.com/TestApplication1/Page.aspx
I need in first case www.domain.com/ and in second example www.domain.com/TestApplication1/
Please guide how this can be achieved.
Thanks
Upvotes: 0
Views: 851
Reputation: 2157
Use this:
Public Function GetAppUrl() As String
Dim uri As Uri = HttpContext.Current.Request.Url
Dim baseUrl As String = uri.Scheme & "://" & uri.Authority
If baseUrl.EndsWith("/") Then baseUrl = baseUrl.Substring(0, baseUrl.Length - 1)
baseUrl &= HttpContext.Current.Request.ApplicationPath
If Not baseUrl.EndsWith("/") Then baseUrl &= "/"
Return baseUrl
End Function
Upvotes: 0
Reputation: 828
Look into the HttpRequest.ServerVariables Property to access IIS Server Variables. You can utilize that to achieve what you're trying to do:
string output = string.Empty;
string httpHost = Request.ServerVariables["HTTP_HOST"];
string url = Request.ServerVariables["URL"];
string[] splitUrl = url.Split('/');
for (int i = 0; i < splitUrl.Length -1; i++)
{
if (splitUrl[i].Length > 0)
{
output += string.Format("/{0}", splitUrl[i]);
}
}
string ssl = "http://";
if (Request.ServerVariables["HTTPS"] == "off")
{
ssl = "https://";
}
output = string.Format("{0}{1}{2}/", ssl, httpHost, output);
Sample output:
http://localhost:60454/WebSite1/Folder/
Upvotes: 0
Reputation: 4585
Uri uri = new Uri(Request.Url.ToString());
string[] segments = uri.Segments;
string result = string.Format("{0}://{1}",uri.Scheme, uri.Host);
segments.ToList().ForEach(s =>
{
if(s!= segments[segments.Length-1])
result += s;
});
You will find the expected result assigned to variable result
.
Upvotes: 0
Reputation: 544
try this:
Dim pos As Integer = Request.Url.AbsoluteUri.LastIndexOf("/")
Dim url As String = Request.Url.AbsoluteUri.Substring(0, pos + 1)
Upvotes: 0
Reputation: 41236
I'm assuming you're running this code from the context of the currently executing web application. If that is the case, then this should do the trick:
var pageSegments = Request.CurrentExecutionFilePath.Split('/');
var pageFolder = "/" + string.Join("/", pageSegments.Take(pageSegments.Length - 1)) + "/";
So given a url of
/Test/SomeFolder/Test.aspx
You should get back
/Test/SomeFolder/
Upvotes: 1
Reputation: 22224
Why not fetch the URL and substring it from the last /
symbol found in the string?
Upvotes: 1