I have a Asp:Hyperlink in aspx page and i am setting the text and navigation url dynamically but when page renders it adds the relative path of my website in the rendered href. i dont know why?
ASPX
<asp:HyperLink runat="server" ID="charityNameText"></asp:HyperLink>
CODE-BEHIND (Page Load Event)
//Getting data from database
charityNameText.Text = entryDetails.RegisteredCharityName;
charityNameText.NavigateUrl = "www.facebook.com";
charityNameText.Target = "_blank";
Rendered HTML
<a id="ctl00_PageContent_CompetitionsEntries_ctl06_charityNameText" href="../../ConLib/Custom/www.facebook.com" target="_blank">save the childrens</a>
../../ConLib/Custom/ is the path where this file is located.
Plase help
There are different solutions for your case.
My best approach would be using the System.UriBuilder class.
String myUrl = "www.facebook.com";
UriBuilder builder = new UriBuilder(myUrl);
charityNameText.NavigateUrl = builder.Uri.AbsoluteUri;
The UriBuilder adds the protocol (HTTP) in your case to the URL you are loading and initializes an instance of the Uri class with the complete URL. Use the AbsoluteUri property.
For more complex cases you can use Regex :
String myUrl = "www.facebook.com";
System.Text.RegularExpressions.Regex url = new System.Text.RegularExpressions.Regex(#"/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.MatchCollection matches = url.Matches(myUrl);
foreach (System.Text.RegularExpressions.Match match in matches)
{
string matchedUrl = match.Groups["url"].Value;
Uri uri = new UriBuilder(matchedUrl).Uri;
myUrl = myUrl.Replace(matchedUrl, uri.AbsoluteUri);
}
You have to add the protocol to the beginning of the URL:
http://wwww.facebook.com
I think you should use http://www.facebook.com
Hope that helps
Related
I want Separating a part of the current URL
Example:localhost:50981/Admin/AddCustomer.aspx
The part I want: AddCustomer
or
Example:localhost:50981/Request/Customer.aspx
The part I want: Customer
You can use AbsolutePath in your onLoad function of page.
//AddCustomer or Customer
string yourPath = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last().Split('.')[0];
You can make use of string.Split():
var url = "localhost:50981/Admin/AddCustomer.aspx";
var result = url.Split('/').Last().Split('.')[0];
To get the current Url path in Asp.Net:
var url = HttpContext.Current.Request.Url.AbsolutePath;
Note:
If you are interested in how to get the different parts of an url have a look at this answer:
var scheme = Request.Url.Scheme; // will get http, https, etc.
var host = Request.Url.Host; // will get www.mywebsite.com
var port = Request.Url.Port; // will get the port
var path = Request.Url.AbsolutePath; // should get the /pages/page1.aspx part, can't remember if it only get pages/page1.aspx
Using method bellow in my MVC view I am getting URL like "localhost:54871/Home/Index" but I don't want "Index" on last. It should be only "localhost:54871/Home". How should I implement this?
#(Request.Url.AbsoluteUri)
Thank you for your help!
While using Request.Url.AbsoluteUri the url changes based on the user's current page.
If user visits http://localhost:0000/Home/ then AbsoluteUri is http://localhost:0000/Home/
If user visits http://localhost:0000/Home/Index then AbsoluteUri is http://localhost:0000/Home/Index
In MVC url generation is based on the route definition defined in RouteConfig.cs file.
You can define route just for controller name, check this and this
Also there is another solution as below:
#{
Uri uriAddress = new Uri(Request.Url.AbsoluteUri);
string urlLeftPart = uriAddress.GetLeftPart(UriPartial.Authority);
string controllerName = HttpContext.Current.Request.RequestContext.RouteData.
Values["controller"].ToString();
string finalURL = urlLeftPart + "/" + controllerName;
}
Url Without Action Name
References
1.
2.
You can try the below one.
For Example if the url with parameters are like below
http://localhost:61221/Websitetest/Default1.aspx?QueryString1=1&QueryString2=2
Then by using the below code you can get the results
HttpContext.Current.Request.Url.Host
HttpContext.Current.Request.Url.Authority
HttpContext.Current.Request.Url.Port
HttpContext.Current.Request.Url.AbsolutePath
HttpContext.Current.Request.ApplicationPath
HttpContext.Current.Request.Url.AbsoluteUri
HttpContext.Current.Request.Url.PathAndQuery
will provide the output like below
OUTPUT
localhost
localhost:61221
61221
/Websitetest/Default1.aspx
/Websitetest
http://localhost:61221/Websitetest/Default1.aspx?QueryString1=1&QueryString1=2
/Websitetest/Default1.aspx?QueryString1=1&QueryString2=2
I have URL need to replace partial url with another url.
Original Url
http://oldurl/dept/it/Lists/Contract Management System/DispForm.aspx?ID=4
I want to replace just http://oldurl (only) with http://www.newurl.com. Rest of the url will change dynamically.
Request.oldURL.GetLeftPart(UriPartial.Authority) will return your base url string which is http://oldurl
string oldURL = "http://oldurl/bs/blabla";
string newURL = oldURL.Replace(Request.oldURL.GetLeftPart(UriPartial.Authority), "http://www.newurl.com");
See more: https://msdn.microsoft.com/en-us/library/system.uri.getleftpart(v=vs.110).aspx
I can get my browser url using : string url = HttpContext.Current.Request.Url.AbsoluteUri;
But say if I have a url as below :
http://www.test.com/MyDirectory/AnotherDir/testpage.aspx
How would I get the "MyDirectory" part of it, is there a utility in .NET to get this or do I need string manipulation ?
If I do string manipulation and say anything after first instance of "/" then wouldnt it return the slash after http:? It would work if my url was www.test.com/MyDirectory/AnotherDir/testpage.aspx
Can someone please help
Instantiate a Uri instance from your url:
Uri myUri = new Uri("http://www.test.com/MyDirectory/AnotherDir/testpage.aspx");
You can then get the path segments into a string array using:
string[] segments = myUri.Segments
Your first "MyDirectory" folder will be at:
string myFolderName = segments[0];
You can get this by PathAndQuery property of Url
var path = HttpContext.Current.Request.Url.PathAndQuery;
it will return /MyDirectory/AnotherDir/testpage.aspx
Uri uriAddr = new Uri("http://www.test.com/MyDirectory/AnotherDir/testpage.aspx");
var firstSegment= uriAddress.Segments.Where(seg => seg != "/").First();
I have a base URL :
http://my.server.com/folder/directory/sample
And a relative one :
../../other/path
How to get the absolute URL from this ? It's pretty straighforward using string manipulation, but I would like to do this in a secure way, using the Uri class or something similar.
It's for a standard a C# app, not an ASP.NET one.
var baseUri = new Uri("http://my.server.com/folder/directory/sample");
var absoluteUri = new Uri(baseUri,"../../other/path");
OR
Uri uri;
if ( Uri.TryCreate("http://base/","../relative", out uri) ) doSomething(uri);
Some might be looking for Javascript solution that would allow conversion of urls 'on the fly' when debugging
var absoluteUrl = function(href) {
var link = document.createElement("a");
link.href = href;
return link.href;
}
use like:
absoluteUrl("http://google.com")
http://google.com/
or
absoluteUrl("../../absolute")
http://stackoverflow.com/absolute