i want to convert a network path (Directory "\www.dummy.com#ABC\test") to URI:
var uri = new Uri(path, UriKind.RelativeOrAbsolute);
It occures a URIFormatException
How can i fix this?
I think you need to change UriKind enum to the Relative value.
This code snippet is working fine:
const string path = #"\www.dummy.com#ABC\test";
var uri = new Uri(path, UriKind.Relative);
Console.Write(uri);
// Output:
// \www.dummy.com#ABC\test
Related
What's the most efficient way to get a specific parameter from a relative URL string using C#?
For example, how would you get the value of the ACTION parameter from the following relative URL string:
string url = "/page/example?ACTION=data&FOO=test";
I have already tried using:
var myUri = new Uri(url, UriKind.Relative);
String action = HttpUtility.ParseQueryString(myUri.Query).Get("ACTION");
However, I get the following error:
This operation is not supported for a relative URI.
int idx = url.IndexOf('?');
string query = idx >= 0 ? url.Substring(idx) : "";
HttpUtility.ParseQueryString(query).Get("ACTION");
While many of the URI operations are unavailable for UriKind.Relative (for whatever reason), you can build a fully qualified URI through one of the overloads that takes in a Base URI
Here's an example from the docs on Uri.Query:
Uri baseUri = new Uri ("http://www.contoso.com/");
Uri myUri = new Uri (baseUri, "catalog/shownew.htm?date=today");
Console.WriteLine(myUri.Query); // date=today
You can also get the current base from HttpContext.Current.Request.Url or even just create a mock URI base with "http://localhost" if all you care about is the path components.
So either of the following approaches should also return the QueryString from a relative path:
var path = "catalog/shownew.htm?date=today"
var query1 = new Uri(HttpContext.Current.Request.Url, path).Query;
var query2 = new Uri(new Uri("http://localhost"), path).Query;
*EDIT: This doesn't happen on Windows but on Mono 4.2.2 Linux (C# Online Compiler).
I want to parse the protocol-relative URL and get the host name etc. For now I insert "http:" to the head before processing it since C# Uri class couldn't handle a protocol-relative URL. Could you tell me if there's any better way or any good library?
// Protocol-relative URL
var uriString = "//www.example.com/bluh/bluh.css";
var uri = new Uri(uriString);
Console.WriteLine(uriString); // "//www.example.com/bluh/bluh.css"
Console.WriteLine(uri.Host); // "Empty" string
// Absolute URL
var fixUriString = uriString.StartsWith("//") ? "http:" + uriString : uriString;
var fixUri = new Uri(fixUriString);
Console.WriteLine(fixUriString); // "http://www.example.com/bluh/bluh.css"
Console.WriteLine(fixUri.Host); // "www.example.com"
This works:
Uri uri = null;
if(Uri.TryCreate("//forum.xda-developers.com/pixel-c", UriKind.Absolute, out uri))
{
Console.WriteLine(uri.Authority);
Console.WriteLine(uri.Host);
}
returns
forum.xda-developers.com
forum.xda-developers.com
It also worked for me using the Uri(string) constructor.
I try to build the following uri
http://localhost:8080/TestService.svc/RunTest
I do it as following
var uriBuilder = new UriBuilder();
uriBuilder.Host = "localhost:8080/TestService.svc";
uriBuilder.Path = String.Format("/{0}", "RunTest");
string address = uriBuilder.ToString()
//In debugger the address looks like http://[http://localhost:8080/TestService.svc]/RunTest
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(address);
The above generates an exception
Invalid URI: The hostname could not be parsed.
I`ll appreciate your help in solving the issue
When using the Uri builder you need to put the host, port & path as it's own line. Also TestService.svc is also part of the path and not the host, you can get away with it if not using port but with port they have to be separated out.
var uriBuilder = new UriBuilder();
uriBuilder.Host = "localhost";
uriBuilder.Port = 8080;
uriBuilder.Path = String.Format("/{0}/{1}", "TestService.svc", "RunTest");
var address = uriBuilder.ToString();
When I run your code, I also see square brackets as the value for the address variable as you point out, but I don't see PerfTestService in the generated Uri, nor do I see why this would be?! I see:
http://[localhost:8080/TestService.svc]/RunTest
Since you already know the host and the path, I suggest you construct it as a string.
var uriBuilder = new UriBuilder("http://localhost:8080/TestService.svc/RunTest");
string address = uriBuilder.ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
I have a Uri object - what property of it will give me the relative path? Of how can I decipher the relative path the file with this Uri. I am coding in c#.
use the Uri.MakeRelativeUri Method (System)
straight from MSDN:
// Create a base Uri.
Uri address1 = new Uri("http://www.contoso.com/");
// Create a new Uri from a string.
Uri address2 = new Uri("http://www.contoso.com/index.htm?date=today");
// Determine the relative Uri.
Console.WriteLine("The difference is {0}", address1.MakeRelativeUri(address2));
furthermore, if you are always looking for the relative path from the root of the domain you could also use myUri.AbsolutePath
Here's a screenie of the Uri debug view with two examples of MakeRelativeUri at the bottom using the following Uri objects
Uri myUri = new Uri("http://msdn.microsoft.com/en-us/library/system.uri.makerelativeuri.aspx#Y600");
Uri myHost = new Uri("http://msdn.microsoft.com/");
Uri myHost2 = new Uri("http://msdn.microsoft.com/en-us/");
You can add a relative path to the Uri like below.
var Url = new Uri("/something/test", UriKind.Relative);
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/"