我知道如何在cookie中保存一些对象数组并使用json将其恢复,但如何从Request.Cookies中使用C#代码获取此数组?
function onAddToBasket(id, price) {
var items = getItems()
items.push({ ID: id, Price: price });
$.cookie("ubasubasket", JSON.stringify(items), { path: '/' });
}
//Getting date from cookies
function getItems() {
var items = [];
if ($.cookie('ubasubasket') != undefined) {
items = JSON.parse($.cookie('ubasubasket'));
}
return items;
}
//C# action in controller
public void Test()
{
var r2 = Newtonsoft.Json.JsonConvert.DeserializeObject(Request.Cookies["ubasubasket"].Value);
/*And here i get ERROR:
{"Unexpected character encountered while parsing value: %. Path '', line 0, position 0."} */
}
答案 0 :(得分:0)
怎么样
public void Test()
{
MyCookie = Request.Cookies["ubasubasket"];
//Grab all values for single cookie into an object array.
String[] arr2 = MyCookie.Values.AllKeys;
//Loop through cookie Value collection and print all values.
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
Response.Write("Value" + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");
}
}