I am using a simple statement
NameValueCollection nvCollection = HttpUtility.ParseQueryString(queryString);
I am trying to grab the values passed in the query string. A simple example of something passed would be
form_id=webform_client_form_4&referrer=http://myURL/webform/request-information?agent=agent&keyword=keyword&full_name=Jon Harding&company=RTS Financial - TEST&phone=913-555-5555
The issue I am having is that the referrer is actually the full string after it. I do need the other values. In this case becauase the ParseQueryString splits the string at the & character the first value is always ignored in the NameValueCollection
Currently when I try to get the value of the agent it is a blank value, understandably.
What would be the best method to make sure I grab all variables? I could split the string at the question mark and then prepend an ampersand to the string before I do the ParseQueryString. Is there a more elegant want to do this?
This will work with least effort:
NameValueCollection nvCollection = HttpUtility.ParseQueryString(queryString.Replace("?","&"));
Related
I'm using a get request to process a search query -- imagine having a search box on the front end to find relevant messages that contain the words given in the search box. My route looks something like this
api/messages/search/?foo%20bar
When I try to handle this on the back end it seems that it is always expecting me to have given it some kind of key-value pair.
What is the appropriate way to handle this kind of query? I want to be able to split the words in the query on %20 and then iterate over them thusly.
What am I missing?
"When I try to handle this on the back end it seems that it is always
expecting me to have given it some kind of key-value pair."
This is correct, because you used the "?" sign you must provide key value pairs after. A query string must have named parameters
api/messages/search/?myString=foo%20bar
See:
https://en.wikipedia.org/wiki/Query_string
I am stumped on this scenario. Basically I have an URL that is passed to a aspx page and then I try to get query string from the URL, but what happens is when I try to get the query string from the URL it omits the '+' and replaces it with an whitespace.
My URL = http://localhost:3872/Test.aspx?mt=jan1TNIixxA1+8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet+q7rq7FoTJf+S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s+0R7vW8I0S9d765RHdYU2xkRuojHYZU=
Request["mt"] =jan1TNIixxA1 8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet q7rq7FoTJf S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s 0R7vW8I0S9d765RHdYU2xkRuojHYZU=
As you can see these two strings are different.
I thought it was the object to string conversion but this does not seem to be the case cause the value of the object has the omitted '+' before conversion.
What can be done to avoid this character replacement (I want to try and avoid string manipulation)
Also what could the cause of this be?
you are getting that because + is the url encoded representation of space " ". If you want to preseve the plus sign in your value you will need to url encode it:
Send that querystring in URL encoded formate and then you will get the expected result.
see: why Request.QueryString replace + with empty char in some cases?
You can use
MyUrl = MyUrl.Replace("+",#"%2B");
The problem is the '+' character is being converted to whitespace if you use httprequest. If you convert it to its hex value, you can pass it with no problem.
You should use HttpUtility.UrlEncode to generate you parameter value. Currently it seems you are using base64 encoding which is not optimal for query parameters.
Use this:
mt=encodeURIComponent(mt);//if mt be --> jan1TNIixxA1+8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet+q7rq7FoTJf+S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s+0R7vW8I0S9d765RHdYU2xkRuojHYZU=
Response.Redirect("Test.aspx?"+mt);
this will encode your URL and after this '+' will converted to "%2B" and if you want to read encoded URL it will not converted to space.
from here
If it is really so important to avoid changing the string when you send it, you could chanhe it AFTER you get ir from httprequest. Maybe you could use:
MyUrl = (Request["mt"].Replace(" ","+"));
There is no possibility to pass the space in url, so when you have a space, you can be sure that there was a '+' in there.
You can get the query string using following method
string strQuery = Request.Url.Query;
I have a situation where I need to put a url inside a GET variable.
for example:
'http://www.site.com/StepOne?b=1&afterUrl=http://www.site.com/StepTwo?someVar=text&b=1'
In this case, when I am at StepOne the b param value will be 1,1 and not 1 as expected.
the afterUrl param will be:
'http://www.site.com/StepTwo?someVar=text'
instead of this:
'http://www.site.com/StepTwo?someVar=text&b=1'
How do I isolate the afterUrl param so its own GET variables won't effect the entire URL.
When you are creating the afterUrl URL parameter, be sure to UrlEncode() the value.
e.g.
var url = String.Format("http://www.site.com/StepOne?b={0}&afterUrl={1}", b, Server.UrlEncode(afterUrl));
Consider using HttpUtility.UrlEncode() for the AfterURL
(EDIT or Server.URLEncode() as others have pointed out)
"http://www.site.com/StepOne?b=1&afterUrl=" +
HttpUtility.UrlEncode(http://www.site.com/StepTwo?someVar=text&b=1");
Then when you finally hit the "StepOne" page you can use HttpUtility.UrlDecode(AfterURL variable name). From there you can Response.redirect or whatever you want with the preserved after url.
Use Server.UrlEncode on someVar to escape out it's querystring values before putting it in the link. You may need to use Server.UrlDecode on the other side to convert it back to the original characters.
Quite simply, you need to URL encode the afterUrl param (actually, you should URL encode all parameters passed to a server), which will turn "http://www.site.com/StepTwo?someVar=text&b=1" into "http%3A%2F%2Fwww.site.com%2FStepTwo%3FsomeVar%3Dtext%26b%3D1", which won't affect the set of parameters. Almost any server framework on the market will automatically decode that back into the string "http://www.site.com/StepTwo?someVar=text&b=1", or at least give you a function to do so yourself.
EDIT:
As this SO question shows, it is possible to URL encode a string without using System.Web, using System.Net.Uri.EscapeDataString().
I have a C# custom webpart on a sharepoint 2007 page. When clicking on a link in an SSRS report on another page, it sends the user to my custom webpart page with a query string like the following:
?tax4Elem=Docks%20&%20Chargers&ss=EU%20MOVEX&Phase=1&tax3Elem=Play%20IT&tax5Elem=Charger
Take note of the value for "tax4Elem", which is basically "Docks & Chargers". (The ampersand can actually come up in "tax4Elem", "tax3Elem", and "tax5Elem").
I cannot have the ampersand in that value encoded so I will have to work with this.
How do I parse this query string so that it doesn't recognize the "&" in "Docks & Chargers" as the beginning of a key/value pair?
Thanks in Advance!
kate
If you really cannot correct the URL, you can still try to parse it, but you have to make some decisions. For example:
Keys can only contain alphanumeric characters.
There are no empty values, or at least, there is always an equal sign = after the key
Values may contain additional ampersands and question marks.
Values may contain additional equal signs, as long as they don't appear to be part of a new key/value pair (they are not preceded with &\w+)
One possible way to capture these pairs is:
MatchCollection matches = Regex.Matches(s, #"\G[?&](?<Key>\w+)=(?<Value>.*?(?=$|&\w+=))");
var values = matches.Cast<Match>()
.ToDictionary(m => m.Groups["Key"].Value,
m => HttpUtility.UrlDecode(m.Groups["Value"].Value),
StringComparer.OrdinalIgnoreCase);
You can then get the values:
string tax4 = values["tax4Elem"];
Note that if the query string is "invalid" according to our rule, the pattern may not capture all values.
I think you can't parse that string correctly - it has been incorrectly encoded. The ampersand in "Docks & Chargers" should have been encoded as %26 instead of &:
?tax4Elem=Docks%20%26%20Chargers&ss=EU%20MOVEX&Phase=1&tax3Elem=Play%20IT&tax5Elem=Charger
Is it possible to change the code that generated the URL?
Obviously the request is incorrect. However, to work-around it, you can take the original URL, then find the IndexOf of &ss=. Then, find the = sign immediately before that. Decode (with UrlDecode) then reencode (with UrlEncode) the part between the = and &ss= (the value of tax4Elem). Then, reconstruct the query string like this:
correctQueryString = "?tax4Elem=" + reencodedTaxValue + remainderOfQueryString
and decode it normally (e.g. with ParseQueryString) into a NameValueCollection.
Or you can use HttpServerUtility.HtmlDecode method to decode the value to '&' (ampersand) sign
Ok, I have a string of the form
string temp = "http://www.example.com?file=666111&submitter=Betty&origin=Office&telNo=05555";
what I need to do is extract the value of the file variable in order to use it. If this was the referrer url I could've done Request.QueryString and got it but the problem is that I have it as a string variable.
I could try to do substring and get the value but I was hoping there was a cleaner way to do this?
Perhaps you can use the HttpUtility.ParseQueryString method. It returns a NameValueCollection with all parameters.