I am using the below code to assign the iframe src through javascript.It's working fine. But in c# code behind i got the query string like this. id=Y&%3bcust_id=100&%3. How can i reduce this.Now
var value = "validity.aspx?id=Y&cust_id=" + cust_id + "";
frameElement.src = value
i want to get the value of customer from query string but it always return null.
if(Request.QueryString["cust_id"] !=null) //It returns null
You need to encode your "&" caracters. For some reason, the server is reading them as &%3b.
Try this:
var value = "validity.aspx?id=Y&cust_id=" + cust_id + "";
value = encodeURIComponent(value);
frameElement.src = value
You need to decode the URL using HttpUtility.HtmlDecode
Try something like this:
var value = HttpUtility.HtmlDecode(URL);
Related
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");
I've passed a really long Query String from one page to another in my Windows Phone 8 project.
I need to pass these parameters from the new page to another page but don't want to reconstruct he entire QueryString.
Is there a way to assign the entire QueryString to a new String?
Something like
String newQuery = NavigationContext.QueryString.ToString();
I need to pass these parameters from the new page to another page but
don't want to reconstruct the entire QueryString
Why not? This is programming: do all the work in one place so you don't have to do it again later. Let's use an extension method to do this.
Silverlight
Place this code in a static class...
public string ToQueryString(this IDictionary dict)
{
string querystring = "";
foreach(string key in dict.AllKeys)
{
querystring += key + "=" + dict[key] + "&";
}
return querystring;
}
Use it like this...
string QueryString = NavigationContext.QueryString.ToQueryString();
ASP.NET
When I originally read this question, I thought it was for ASP.NET, not Silverlight. I'll leave the ASP.NET answer here in case someone stumbles across it looking for how to do it in ASP.NET.
public string ToQueryString(this NameValueCollection qs)
{
string querystring = "";
foreach(string key in qs.AllKeys)
{
querystring += key + "=" + qs[key] + "&";
}
return querystring;
}
Use it like this...
string QueryString = Request.QueryString.ToQueryString();
There is something that already exists for ASP.NET. But I feel it's important to demonstrate that you can do all the work once somewhere. Then not have to do it again. If you want to use a built-in way, something like this would work, using the Query property of the Uri class.
string QueryString = System.Web.HttpContext.Current.Request.Url.Query;
Here's a way that may be a little simpler...
You could project the results into a format of your choosing. Here's a simple example below.
I've used an IDictionary<string,string> as it is the underlying type for NavigationContext.QueryString
var test = new Dictionary<String,String>();
test.Add("1", "one");
test.Add("2", "two");
test.Add("3", "three");
// Choose any string format you wish and project to array
var newArray = test.Select(item => item.Key + ":" + item.Value).ToArray();
// Join on any separator
string output = String.Join(",", newArray);
This still means that you have to interpret the result later (according to the format you chose). Here you'll get a format like
"1:one,2:two,3:three"
If you've sent it as a querystring just pull it back out on the OnNavigatedTo() Method and then you can store the query in the page until you move on?.
string newQuery;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
newQuery = NavigationContext.QueryString["queryName"];
}
Try this:
public string GetQueryString()
{
IDictionary<String, String> NavigationContextData = NavigationContext.QueryString;
string data = "/Pagename.xaml?";
foreach (var item in NavigationContextData)
{
data += item.Key + "=" + item.Value + "&";
}
data = data.Substring(0, data.Length - 1);
return data;
}
If it's in your OnNavigatedTo() event, you can use a quick, easy two-liner. This can be condensed to a single line or expanded to check for the existence of the ? character. If you know that there are always parameters passed, the check is unnecessary and these two lines work fine:
string QStr = e.Uri.ToString();
string ParmStr = QStr.Substring(QStr.IndexOf('?') + 1);
You can also condense it into a single line:
string ParmStr = e.Uri.ToString().Substring(e.Uri.ToString().IndexOf('?') + 1);
I have a problem with getting a 'keyword' from this string, i tried string.replace() but it didn't work, has anyone any idea, how separate keyword from this string?
var url = "< id xmlns=\"http://www.w3.org/2005/Atom\">http://gdata.youtube.com/feeds/api/videos/keyword< /id>";
Thanks for help!
While you work with xml document, it will be easy to get values elements:
var xml = "<id xmlns=\"http://www.w3.org/2005/Atom\">http://gdata.youtube.com/feeds/api/videos/keyword</id>";
var url = XElement.Parse(xml).Value;
var index = url.LastIndexOf('/') + 1;
var keyword = url.Substring(index);
If you always need just last segment you can easily achieve with Url instance:
var keyword = new Uri(url).Segments.Last();
Thanks #Alexei
var url = "< id xmlns=\"http://www.w3.org/2005/Atom\">http://gdata.youtube.com/feeds/api/videos/keyword< /id>";
string[] splitArra = url.Split(new char[]{'/','<'});
string keywordString = splitArra[11];
I'm sure there's a better and cleaner way to this but this should work:
string keyword = url.Substring((url.IndexOf("videos/")) + 7,url.Length - url.IndexOf("< /id>")+1);
Or this:
string keyword = url.Substring(83, url.Length - url.IndexOf("< /id>") + 1);
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.
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=