How to get param in link by HTTP GET ASP.NET C# - 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");

Related

How to get a parameter from a URL encoded?

I have the following code to get parameters and their key from a URL:
string queryString = new Uri(URL).Query;
var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);
var paramsList = new Dictionary<string, string>();
foreach (var parameter in queryDictionary)
{
var key = (string)parameter;
var value = System.Web.HttpUtility.ParseQueryString(queryString).Get(key);
}
It works perfect. Exception, in value, I have the decoded value. I would need to have it as it was in the address, before it was decoded. How can I do it? Note that to encoded it after gives different value in some cases.
The encoding is happening in your first line already:
string queryString = new Uri(URL).Query;
Presumably you want to avoid writing your own code to extract the Query part from a URL. Which is sensible. You can still rely on the Uri class to do the parsing for you:
var uri=new Uri(URL);
var queryString= URL.Replace(uri.GetLeftPart(UriPartial.Path), "").TrimStart('?');
———————————————————————————————————
(
var URL="http://a/b/?d= #£$%% sdf we 456 7 5?367";
var uri=new Uri(URL);
Console.WriteLine(uri.Query); // Already url-encoded by the Uri constructor
var queryString = URL.Replace(uri.GetLeftPart(UriPartial.Path), "").TrimStart('?');
Console.WriteLine(System.Web.HttpUtility.ParseQueryString(queryString)); //Not encoded!
)
(As an aside, LinqPad helps)
A possible way of doing this is to encode it back:
var encodedValue = HttpUtility.UrlEncode(value);
ASP.NET automatically calls HttpUtility.UrlDecode when you access the query string.
This is a bit dirty, but should work fine.

How to add data in array of strings in C#

I have a text box where I'm getting url, for ex:
http://www.amazon.com/black-series-650.
Now I would like to add /en black-series-650 an it looks like.My ouput should be like this.
http://www.amazon.com/en/black-series-650.
With this code you can apply for url like :
http://www.amazon.com/black-series-650/abc/ed/aaa
You can try this code :
var url = "http://www.amazon.com/black-series-650";
// var url = txt_OriginalUri.Text;
var tempUrl = url.Replace("http://", string.Empty);
var lastSlashIndex = tempUrl.IndexOf('/');
var resultUrl = "http://" + tempUrl.Insert(lastSlashIndex + 1, "en/");
var tmp = OriginalUri.Text;
var en = tmp.Replace("http://www.amazon.com", "http://www.amazon.com/en");
var lst = new List<string>();
lst.Add(OriginalUri.Text);
lst.Add(en);
Instead of dealing urls with Split function you should parse your url using System.Uri. Check the url segments and re-construct as per your need.
This should work for you:
string str = "http://www.amazon.com/black-series-650";
str = str.Insert(str.LastIndexOf("/"),"/en");
You could also try to find the last occurence by using string.LastIndexOf instead of splitting:
string url = txt_OriginalUri.Text;
int idx = url.LastIndexOf('/');
string newUrl = url.Insert(idx,"/en");

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=

Get url parts without host

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

Categories