include value with '&' in querystring [duplicate] - c#

This question already has answers here:
How to build a query string for a URL in C#?
(39 answers)
Closed 9 years ago.
I have to write this data in querystring:
http://localhost:1256/4.market.ph.local/WEP/Add.cshtml?data=me+&+you
I got an error because of that symbol '&' i used.

In c# you can use this:-
HttpUtility.UrlEncode("http://localhost:1256/4.market.ph.local/WEP/Add.cshtml?data=me+&+you");
HttpUtility is a part of System.Web and this will ensure an of the non permitted query string char are url Encoded.
Once you do this you will get something like this http%3a%2f%2flocalhost%3a1256%2f4.market.ph.local%2fWEP%2fAdd.cshtml%3fdata%3dme%2b%26%2byou
On the receiver just decode it back.

Use urlencode($yourstring) or if you are hard coding it, use %26 to represent the ampersand.

Related

C# - How to send search keywords like google [duplicate]

This question already has answers here:
Encoding parameters for a URL
(3 answers)
Closed 4 years ago.
I want to send search keywords with QueryString like google, atc...
and i need a function which convert text like this
Metallica nimes concert 2009 :)
to this:
Metallica+nimes+concert+2009+%3A%29
and retrieve it
Thanks and advance
you need to encode the string using:
HttpUtility.UrlEncode("Metallica nimes concert 2009 :)")
you'll need to reference to System.Web
This will work on simple strings, if you want to encode more characters see:
Server.UrlEncode vs. HttpUtility.UrlEncode. as Panagiotis Kanavos recommended
Trying to encode a string into a URL?
Why not use system.net.webutility.urlencode
Docs here
Rock here

Is there a native way to parse key/value pairs into an array? [duplicate]

This question already has answers here:
Get URL parameters from a string in .NET
(17 answers)
Closed 7 years ago.
I have a string that is the contents of a webpage in this format :
value=foo&value1=bar&value2=foobar
What I would like to know is, is there a built-in way to convert this into an object/list of object/whatever so that I can loop through or access the values by keyname
I know I can split on '&', and then split on '=', but I believe there is a built-in way to do this, I just do not know what it is.
The language I am working in is C#.
You can use HttpUtility.ParseQueryString.
Eg.
NameValueCollection values = HttpUtility.ParseQueryString("value=foo&value1=bar&value2=foobar");

C# string replace for website names [duplicate]

This question already has answers here:
How do I write a backslash (\) in a string?
(6 answers)
Closed 7 years ago.
I am trying to do a string replace for website names. Here is the code:
string output = input.Replace("C:\Design\Website\", "Someting");
TextBox.Text = osc.output;
The code is incorrect as there is an issue \ with these marks. How can I fix my code?
You need to escape the \:
Either:
"C:\\Design\\Website\\"
Or:
#"C:\Design\Website\"

What does "#" mean before a string? [duplicate]

This question already has answers here:
What's the # in front of a string in C#?
(9 answers)
Closed 8 years ago.
What does "#" mean before a string?
I saw this notation with paths:
string myfolder = #"C:\Users\";
But also with normal strings.
It means it's a literal string, so won't treat \ as an escape character, for example. This page should help you understand it better.

Equivalent to Javascript's encodeURI? [duplicate]

This question already has answers here:
Does C# have an equivalent to JavaScript's encodeURIComponent()?
(7 answers)
UrlEncode - Javascript vs. C# [duplicate]
(5 answers)
Closed 9 years ago.
C#'s equivalent to encodeURIComponent is well-covered on SO and elsewhere, but what about encodeURI? Basically I want to encode invalid URL characters only and not reserved characters such as /, :, etc. So
"http://www.example.com/my cool page"
would be encoded to
"http://www.example.com/my%20cool%20page"
Is there something baked into .NET to do this? Or is a regex my best bet?
Try
Uri.EscapeUriString("http://www.mysite.com/my cool page")
Try
Server.URLEncode(uri.ToString)
Try this:
HttpUtility.UrlEncode(String)
For example:
var url_encoded_string = HttpUtility.UrlEncode(userInput);
You could use System.Net.WebUtility.UrlEncode(string value).

Categories

Resources