"&" inside QueryString value - asp.net

I'm using QueryStrings for users to auto-select values from a drop down list. Some of the values in this drop down list contain "&" in them.
E.g. "Acquisitions & Development"
When the user passes through a value with an "&", the string value gets cut off before the second half.
string functionalDepartment = Request.QueryString["FunctionalDepartment"];
//returns "Acquisitions "
Is there a way that I can include the second half of the value when getting the value from the QueryString?

The & character needs to be URL encoded because & is used to divide the query string into name=value pairs. When creating the URL, use:
HttpUtility.UrlEncode(url);

Before passing it to query string just replace:
.Replace("&","%26");
or use Server.URLEncode()

You could user System.Web.HttpUtility.UrlEncode while setting the values for the drop down.
This will ensure that & will not break the values.

Use HttpServerUtility.UrlEncode:
URL encoding ensures that all browsers will correctly transmit text in URL strings. Characters such as a ampersand (&) might be truncated or corrupted by some browsers.
More Details see this Query strings with special characters
I also refer that

Server.UrlEncode("url") will solve ur issue.
Example:
MyURL = "http://www.ursite.com/urpage.aspx?ReqStr=" + Server.UrlEncode("ASP.NET Examples");

Related

Removing unwanted characters from username field using infopath

Can anyone tell me how to remove unwanted characters from username field.
ex: i:0#.w|abcventures\sreekiran.k I need to remove the characters which are generated before abcventures\sreekiran.k.
I have used translate() to eliminate those characters, but, it is removing i & w characters also, from the username.
To expand on the current answers, I think they meant to use substring-after(). This will do the trick:
substring-after(userName(), "i:0#.w|")
To expand on Mekalikot's answer -
substring-before(userName(), "i:0#.w|")
Will return just the string after the "i:0#.w|" from the user name function. If the user name function does not include that string, however, the formula will return nothing. Since you'll get a different value from the user name function in preview vs. the browser, you'll probably need to test this with the published form.
Use substring-before to remove those characters.

Query String Returning Different Values to that of the actual in the url

http://localhost:1079/BattleSimulator.aspx?userID=Unregistered_User&Troops=1111%1111%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0!1111%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0&Research=10%10%10%10%10%10%0!10%10%10%10%10%10%0&Sanctuary=0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0!0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0&Inventory=False%False%False%False%False%False!False%False%False%False%False%False&other=5!0&RNG=0&Dragons=-1%-1%-1%-1!-1%-1%-1%-1&BattleArts=0%0%0!0%0%0&Kaizer=2310000%1510000%0%15867000%910000%875!5011000%2810000%3158%182972948%2810000%803
^ this is the original Query Passed onto server,
But this is parsed on a very different way on the server
Ex:
http://prntscr.com/3h90fs
http://prntscr.com/3h90o2
the string returned from
Extention.QueryString("Troops")
is "111111%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0!1111%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0%0"
Why is this happening ? is it a bug ?
There are many chars that are reserved and can't be used in querystring. % is one of those.
You have to Encode a string to be shure that it will be properly decoded to the same string in the server.
In C# this can be done this way:
HttpContext.Current.Server.UrlEncode(destinationURL);
Why is this happening ?
Because added any(some) spaces(White Character ) between your values.
is it a bug ?
Nope. It's not a bug .
Solution.
You need use Trim() or remove Empty values by using Replace(" ","") in your query string values before send query string
See this links
Get Query String value containing spaces
Passing in a querystring with a space?
http://blogfornet.com/tag/how-to-use-space-in-url-query-string/
Request.QueryString giving me space instead of + sign

Passing Empty string parameter in URL

I want to pass an empty string parameter in a url. I don't know how to do it.
I am passing these parameters in the url for a grid. In the database if I using Race=''. I get the respective records. I need to pass this in the url
test.aspx?race=""
The url would look like this:
text.aspx?race=
or
text.aspx?race=&otherParam=value&etc=otherValue
Then you would have code to handle race being empty:
if(string.IsNullOrEmpty(Request.QueryString["race"]))
// handle the empty race differently
You can pass an empty string in url like this
test.aspx?race=%02%03
%02 - start of text ASCII Character
%03 - end of text ASCII Character
In this page you can find other characters too.
Dim race As String
race = ""
Response.Redirect("page.aspx?race=" & race)
Is this what you want??

asp.Net + encrypted QueryString requested not reading '+' sign

I have an encrypted query string passed from another page, it reads something like "/se73j+sef" but after receiving it, the '+' sign got omitted and became "/se73j sef". Is this normal? Please kindly advice. Thanks.
Is this normal?
Yes, perfectly normal. + is a special character in an url. It means space (0x20 ASCII character). If you want to represent the + sign you will have to url encode it:
/se73j%2Bsef
To url encode a string in .NET you could use the UrlEncode method. Or depending on how you are building the url there are certainly better ways.

How does one pass string contains '\' from from asp.net server side to Javascript function?

How does one pass string contains '\' from from asp.net
server side to javascript function?
After checking parameters at client side, all '\' replaced with '' even,
replacing '\' with '%5C' at server side doesn't work.
Any idea?
\ is a special character - basically it "escapes" the character after it. Try passing \\ instead. BTW - if you're using C# you can use the # character before a string to avoid needing to pass it as a double slash, e.g.
string path = #"c:\documents\mydocuments";
I got solution for that.
parameter.Replace("\\", "\\\\") solve it.
Are you using ASP.NET to write a JavaScript string literal? ie. something like:
Page.RegisterStartupScript("foo",
"<script type='text/javascript'>"+
" var bar= '"+myBarValue+"';"+
"</script>"
);
If so, then you are embedding text inside a delimited JavaScript string literal and you must use an escaping scheme that follows the syntax for string literals. In particular any \ character inside the text must be escaped with \\, and any ' character must be replaced by \', since that's the delimiter being used in this case (JavaScript can use either type of quote to delimit strings).
What's more if you're using an inline <script> block like in the above example, you're actually embedding text in a string literal in an HTML element, so you have to do some HTML escapes too. In particular you have to break up any </ sequences in the text, because that would end the script block. Also, in XHTML, there are no CDATA elements, so you'd also have to ampersand-escape any < or & characters in the text, except that would make it incompatible with legacy-HTML parsers. So to solve all these problems it is better to use JavaScript string literal escapes for that too, replacing < with \x3C and & with \x26.
Ideally what you would do would be to pass the simple string to a JSON encoder library, which would take care of escaping it appropriately for JavaScript. However I don't know of one for .NET that will escape the HTML for you as above, so you'd still need some replaces.

Resources