C# 从Cookie获取对象数组
我知道如何在cookies中保存一些对象数组,并使用json将其取回,但如何从Request.cookies中以C#代码获取该数组C# 从Cookie获取对象数组,c#,jquery,asp.net-mvc,C#,Jquery,Asp.net Mvc,我知道如何在cookies中保存一些对象数组,并使用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
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."} */
}
怎么样
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>");
}
}
公共无效测试()
{
MyCookie=Request.Cookies[“ubasubasaket”];
//将单个cookie的所有值捕获到对象数组中。
字符串[]arr2=mycokie.Values.AllKeys;
//循环执行cookie值集合并打印所有值。
for(loop2=0;loop2”;
}
}
结果是“Value0:”在尝试反序列化“Request.Cookies[“ubasubasaket”].Value”之前,“Request.Cookies[“ubasubasaket”].Value”的内容是什么