Get url parts without host - c#

I have a url like this :
http://www.somesite.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye.
I want to get mypage.aspx?myvalue1=hello&myvalue2=goodbye from it . Can you tell me how can I get it ?

Like this:
new Uri(someString).PathAndQuery

var uri = new Uri("http://www.somesite.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string pathOnly = uri.LocalPath; // "/mypage.aspx"
string queryOnly = uri.Query; // "?myvalue1=hello&myvalue2=goodbye"
string pathAndQuery = uri.PathAndQuery; // "/mypage.aspx?myvalue1=hello&myvalue2=goodbye"

Place your string URL into a URI object and then use the AbsolutePath & Query properties to get the URL parts you need.
Or use the PathAndQuery property to get both, which is what you need.
More information can be found here:
http://msdn.microsoft.com/en-us/library/system.uri_members%28v=VS.71%29.aspx

new Uri(System.AppDomain.CurrentDomain.BaseDirectory).Segments

Related

Get first part from url(first root directory value only) in C#

I have this url (www.site.com/folder1/sub-folder).
Now I just want to get only folder1 from this url. How can I do that?
I'm getting Last root url from below code but I want only first root url.
string s = Page.Request.Url.AbsolutePath;
s = s.Substring(s.LastIndexOf("/")+1);
Please help me to get only first directory value.
Try this:
Uri uri = new Uri("http://www.example.com/folder1/sub-folder");
var segs = uri.Segments;
var folder = segs[1];
You can use Split() method twice like
string str = "www.site.com/folder1/sub-folder";
string folder = (str.Split('/')[1]).Split('/')[0];
You can use Split like:
var value = s.Split(new[]{'/'}, StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault();

How to remove www, http/https and other strings in URL in C#

For Example I have this string
http://www.merriam-webster.com/dictionary/sample
What I want is to return only merriam-webster.com I'm planning to use .Replace() but I think there are better approach for this question.
If you are working for Winforms then
string url = "http://www.merriam-webster.com/dictionary/sample";
UriBuilder ub = new UriBuilder(url);
MessageBox.Show(ub.Host.Replace("www.",""));
and for web,
Get host domain from URL?
How about this
System.Uri uri = new Uri("http://stackoverflow.com/search?q=something");
string uriHost = uri.Host;
?
Answers here don't handle the case like "http://abcwww.com".
Check if the url starts with 'www.' before replacing it.
if (url.StartsWith("www.", StringComparison.OrdinalIgnoreCase))
url = url.Replace("www.", string.Empty);
You can use this:
var a = "http://www.merriam-webster.com/dictionary/sample";
var b = a.Split('.');
var c = b[1] +"."+ b[2].Remove(3);
You can leverage System.Uri class:
System.Uri uri = new Uri("https://www.google.com/search?q=something");
string uriWithoutScheme = uri.Host + uri.PathAndQuery + uri.Fragment;
Or you can use below:
UriBuilder ub = new UriBuilder("https://www.google.com/search?q=something");
currentValue = ub.Host.Replace("www.", "");

How to get param in link by HTTP GET ASP.NET C#

My link is:
http://excample.com/default.aspx?param=1
I want to get "1" in link. And if my link is:
http://excample.com/default.aspx?param1=1&param2=0
Please help me to get the values of param1 and param2. Thank you my friend !!!
I use ASP.NET C#
You can try the below code.
Uri myUri = new Uri("http://excample.com/default.aspx?param1=1&param2=0");
string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("param1");
OR
HttpContext.Current.Request.QueryString.Get("param1");
OR
Request.QueryString["param1"];
In every Request there are Form and QueryString properties.During the Request,in the Form property it contains the values which comes after submiting the form, and in QueryString it contains every parameter passed by the URL.So you need only get the QueryString from the Request and retrieve two parameters like this
var param1 = Request.QueryString["param1"]
var param2 = Request.QueryString["param2"]
You only think like this.Almost everything you need during the request is in the Request property.For parameters from query string they are in the QueryString property.
For deeply knowledge see here.https://msdn.microsoft.com/en-us/library/ms524784(v=vs.90).aspx and https://msdn.microsoft.com/en-us/library/ms525985(v=vs.90).aspx
You can try like this:
var uri = new Uri("http://excample.com/default.aspx?param=1");
var query = HttpUtility.ParseQueryString(uri.Query);
var par = query.Get("param");
or
var uri = new Uri("http://excample.com/default.aspx?param1=1&param2=0");
var query = HttpUtility.ParseQueryString(uri.Query);
var par1 = query.Get("param1");
var par2 = query.Get("param2");

c# rewrite url parameter

I have a simple task without a simple solution.
I have a parameter in the browser that needs to be changed or rewritten
for instance www.contoso.com/countries.aspx?country=UK
all I need is to rewrite the parameter without checking the url so it might appear as:
www.contoso.com/countries.aspx?country=France
I have tried something like that but with no joy
string parameter2 = Request.QueryString["country"];
Context.RewritePath(parameter2.Replace("?country=", "France"));
You could do something like this:
var url = "www.contoso.com/countries.aspx?country={0}";
var country = "UK";
url = String.Format(url, country);
Alternatively you can do:
var url = Request.Url.AbsolutePath;
var country = Request.QueryString["country"];
url = url.Replace(country, "UK");
Then:
Response.Redirect(url);
Can you not read the whole URL into a string, split it on the '?' and then add your new bit to the first part of the string?
Something like this:
var url = Request.QueryString;
var newUrl = url.split('?');
url = newUrl[0] + "?country=France";
I dont know if that will work, its just a thought
If you want to replace the complete querystring, use
newVal = string.LastIndexOf("?");
and then
URL.Replace(oldVal, newVal);
OR if you have just one parameter in querystring and want to replace only value of it, use
newVal = string.LastIndexOf("=");
URL.Replace(oldVal, newVal);
Look at this detailed response for solution to your problem.

QueryString converted to URL-Encode using NameValueCollection

I am passing an encrypted URL string:
Default.aspx?S3tLlnIKKzE%3d
I want to pass that URL string back into the ASPX page in a variable.
protected string qs = string.Empty;
NameValueCollection qscollstring = HttpContext.Current.Request.QueryString;
qs = qscollstring[0];
Which return : S3tLlnIKKzE=
The value in qscollstring[0] is correct: S3tLlnIKKzE%3d
I understand the problem is URL-Encoding, but I cannot find a way to keep the string as is.
It seems that assigning the value from qscollstring[0] is: S3tLlnIKKzE%3d
to string changes the value : S3tLlnIKKzE=
I need to to stay: S3tLlnIKKzE%3d
Use HttpUtility.UrlEncode method to encode the string.
qs =HttpUtility.UrlEncode(qscollstring[0]);
You can also pull the value from the Uri of the current URL without having to Encode the value.
Sample:
Uri u = new Uri("http://localhost.com/default.aspx?S3tLlnIKKzE%3d");
string q = u.Query;
And part of your page:
string q = !String.IsNullOrEmpty(Request.Url.Query) && Request.Url.Query.Length > 1 ? Request.Url.Query.Substring(1) : Request.Url.Query;
Like me if you are searching for the reverse .. use
qs =HttpUtility.UrlDecode("S3tLlnIKKzE%3d");
to get back S3tLlnIKKzE=

Categories