Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode or Decode space that save in cookies in mvc razor

I am creating cookie using jquery and I set value into it. when I am reading that value, it is coming as something like with %20, there spaces has been converted to some thing like that. so how can I get proper value.

here is my Cookie creation code :

 $.cookie('ck_name', 'william smith', { path: '/' });

in razor, I am reading cookie like this :

 Request.Cookies["ck_coursename"].Value.ToString()

Output is : willian%20smith

i tried both @html.raw() and httputility.htmlencode, but problem still persists.

any help ? can be appreciated.. thanks in advance.

like image 710
user2818541 Avatar asked Aug 15 '15 21:08

user2818541


People also ask

Should cookie values be encoded?

It's perfectly acceptable, even if it's not strictly mandatory. There are only a handful of values that must be URL encoded, but just blanket URL encoding everything makes it much easier to work with cookies.

What encoding type is used for cookies?

It is then base64 encoded so it is an ASCII string, since the underlying HTTP protocols expect to work with ASCII. That base64 encoded string becomes the value of the cookie. When cookies are sent back to the server, they are read, (base64) decoded, decrypted, JSON parsed, and stored in memory as key/value pairs.

What is encoding and decoding in C#?

The . NET Framework provides several classes for encoding (converting Unicode characters to a block of bytes in another encoding) and decoding (converting a block of bytes in another encoding to Unicode characters. The System.


1 Answers

The value is URL encoded, so you would use the UrlDecode method to read it:

HttpUtility.UrlDecode(Request.Cookies["ck_coursename"].Value.ToString())
like image 178
Guffa Avatar answered Oct 24 '22 09:10

Guffa