John Lk
John Lk

Reputation: 185

passing "&" string to controller using get method

I am trying to pass string via url GET parameters to my MVC controller. But the problem is whenever inside my string any "&" comes up its just getting skipped the text from there. The reason i found is on http get "&" is reserved for define new parameters. Is there any way to pass string smoothly?

http://localhost:60617/CategoryResearch/Result?page=1&keywords=Baby Safety & Health

Example string is: "Baby Safety & Health"

Upvotes: 0

Views: 923

Answers (4)

Icemanind
Icemanind

Reputation: 48726

Ampersand's are one of many characters that have a special meaning when your browser parses a URL. You can learn more about this from the wiki page.

In order to use these characters in a URL, the URL must be encoded. In C#, you can use UrlEncode to generate an encoded URL string.

Upvotes: 1

Mamunur Rashid
Mamunur Rashid

Reputation: 11

Yes; that's correct. You need to use JSON.stringify while placing the parameters. It will encode "&" and other special characters. For example,

data: {
            emp_id: JSON.stringify(emp_id)              
      },

Upvotes: -2

gmiley
gmiley

Reputation: 6604

As mentioned in the comments, you need to UrlEncode your string before sending it. Take a look at HttpUtility.UrlEncode.

string keyWords = HttpUtility.UrlEncode("Baby Safety & Health");

Upvotes: 1

NicoRiff
NicoRiff

Reputation: 4883

You have to send the & encoded as this:

http://localhost:60617/CategoryResearch/Result?page=1&keywords=Baby Safety %26 Health

Here you have a complete reference on how to encode special characters:

https://www.w3schools.com/tags/ref_urlencode.asp

Upvotes: 2

Related Questions