Cannot parse a correct URI - c#

I have this link: www.axams-freizeitzentrum.com/ruifach-stadion.htm
when I try to create an Uri using this code:
var link = new Uri("www.axams-freizeitzentrum.com/ruifach-stadion.htm");
this will return
Invalid URI: The format of the URI could not be determined.
what is wrong?

Check possible reasons here: http://msdn.microsoft.com/en-us/library/z6c2z492(v=VS.100).aspx
You need to put the protocol prefix in front the address, i.e. in your case "http://"
var link = new Uri("http://www.axams-freizeitzentrum.com/ruifach-stadion.htm");

Uris need a scheme name.
var link = new Uri("http://www.axams-freizeitzentrum.com/ruifach-stadion.htm");

Related

How to get IPaddess from given String

I have String like this:
http://192.168.xx.xx/abc/abcd.php
and want to fetch only ipaddress from it.
The Expected output should be:
192.168.xx.xx
I can do this by splitting it by '/' but is there any easy way out.
Here, give this a try:
var url = "http://192.168.1.1/abc/abcd.php";
Uri uri = new Uri(url);
var ip = Dns.GetHostAddresses(uri.Host)[0];
Console.WriteLine(ip.ToString());
You will need System.Net for this.

C# add scheme to URI

I'm trying to add the HTTP Protocol to this URI "example:8888"
What I've done :
var uriBuilder = new UriBuilder("example:8888")
{
Scheme = Uri.UriSchemeHttp,
};
var uri = uriBuilder.Uri;
The Output is
http:0.0.34.184
What I'm doing wrong :S ?
Your string is being parsed as the URL path, not a hostname.
To force it to parse as a hostname, you need to add a scheme to the string.

'System.UriFormatException' in HyperLink

I have some strings like this:
www.example.com/sdWqaP
twitter.com/sdfks
and want to assign them to a HyperLink
var hyperlink = new Hyperlink
{
NavigateUri = new Uri(url),
TargetName = "_blank",
};
if url starts with http:// it works fine, otherwise throws a UriFormatException.
Update: urls like this www.google.com aren't valid http urls. isn't there a better way than var url = "http://" + "www.google.com"
You can use
var uri = new UriBuilder(s).Uri;
Reference: http://msdn.microsoft.com/en-us/library/y868d5wh(v=vs.110).aspx
public UriBuilder(
string uri
)
// If uri does not specify a scheme, the scheme defaults to "http:".
Scheme (http:// in your case) is mandatory part of Uri string. UriFormatException will be thrown if the scheme specified in uri string is not correctly formed according to Uri.CheckSchemeName() method.
[MSDN : Uri Constructor (String)].
I don't understand well what you mean "better safer way". Appending scheme in uri string is common practice anyway.
Check your URL is valid and then assign to the URL
For validating a URL check the below link
How to check whether a string is a valid HTTP URL?

Assign a URL to Url.AbsoluteUri in ASP.NET

Can I assign a URL to Url.AbsoluteUri in ASP.NET?
No, it's a read-only property.
You can create a new Uri though:
var url = new Uri("http://absoluteurl.example.com");
// url.AbsoluteUri is now "http://absoluteurl.example.com"
No, you can't. But you can create a Uri with the desire AbsoluteUri by constructing one:
Uri url = new Uri("http://www.google.com");
No, you cannot, it is derived from the URI. See the MSDN documentation for Uri.AbsoluteUri.
Example from MSDN
Uri baseUri = new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri,"catalog/shownew.htm?date=today");

.NET URI: How can I change ONE part of a URI?

Often I want to change just one part of a URI and get a new URI object back.
In my current dilemma, I want to append .nyud.net, to use the CoralCDN.
I have a fully qualified URI fullUri. How can I, in effect, do this:
fullUri.Host = fullUri.Host + ".nyud.net";
This needs to work for almost any URL, and the PORT of the request needs to be maintained.
Any help would be much appreciated.
You can use an UriBuilder to modify individual parts of an Uri:
Uri uri = new Uri("http://stackoverflow.com/questions/2163191/");
UriBuilder builder = new UriBuilder(uri);
builder.Host += ".nyud.net";
Uri result = builder.Uri;
// result is "http://stackoverflow.com.nyud.net/questions/2163191/"

Categories

Resources