How to get parameter from url string in c#
Recently i have posted get parameter from url javascript, Check/Uncheck all
checkboxes of a gridview in javascript, Highlighting text inside div in JavaScript and now i am going to explain multiple ways to get parameter from url string or given query data in asp.net.
below is the function named GetQueryParameters, by passing Url String you can get the collection of query string, you can implement this function by two way given below
Or you can use inbuilt HttpUtility Method ParseQueryString.
Calling of get url parameter funtion.
below is the function named GetQueryParameters, by passing Url String you can get the collection of query string, you can implement this function by two way given below
private NameValueCollection GetQueryParameters(string dataWithQuery)
{
NameValueCollection
result = new NameValueCollection();
string[] parts
= dataWithQuery.Split('?');
if
(parts.Length > 0)
{
string
QueryParameter = parts.Length > 1 ? parts[1] : parts[0];
if (!string.IsNullOrEmpty(QueryParameter))
{
string[] p
= QueryParameter.Split('&');
foreach (string s in p)
{
if
(s.IndexOf('=') > -1)
{
string[]
temp = s.Split('=');
result.Add(temp[0], temp[1]);
}
else
{
result.Add(s, string.Empty);
}
}
}
}
return result;
}
Or you can use inbuilt HttpUtility Method ParseQueryString.
private NameValueCollection GetQueryParameters(string dataWithQuery)
{
return HttpUtility.ParseQueryString(dataWithQuery);
}
Calling of get url parameter funtion.
string LinkUrl =
"http://www.dotnetbull.com?post=linkparameter&month=app&day=sat";
//or string
LinkUrl="post=linkparameter&month=app&day=sat";
NameValueCollection
dtQueryString = GetQueryParameters(LinkUrl);
string month =
dtQueryString["month"]; //app