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);
Related
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
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");
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;
How do I use either webclient or httpwebrequest to do two things:
1)Say after downloading the resource as a string using:
var result = x.DownloadString("http://randomsite.com);
there's a relative url(also query string):
Click here to get your name and age
how do I click(follow) on that link using webclient? after initially loading the resource in result. i was able to use htmlagilitypack to isolate the href but I would now like to follow it in code.
2) If the httpwebrequest does not redirect but instead loads the same page with different parameters how would i use webclient to retrieve the new url that is generated?
i.e if i call
var result = x.DownloadString("http://randomsite.com);
but this actually calls
http://randomsite.com/q?site=default
I then want to retrieve the second url
Thanks in advance
You can construct the url from the link and the link that you just downloaded like this:
Uri baseUri = new Uri("http://randomsite.com");
Uri myUri = new Uri(baseUri, "/q?name=john&age=50");
Console.WriteLine(myUri.ToString()); // gives you http://randomsite.com/q?name=john&age=50
This also works if you base Url has url parameters.
As for the second question, i guess you meant that the request was redirected and you want that url instead? Then the easiest way to do so is to sub-class WebClient described here.
Uri baseUri = new Uri("http://randomsite.com");
using(var client=new WebClient())
{
var result = client.DownloadString(myUri);
//get href via HtmlAgilityPack...
Uri myUri = new Uri(baseUri, "/q?name=john&age=50");
result = client.DownloadString(myUri);
}
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/"