[英]Set cookie in Global.asax.cs and get it in the view of asp.net mvc c#
I registered cookie in session start of Global.asax.cs of asp.net mvc project. 我在asp.net mvc项目的Global.asax.cs的会话开始中注册了cookie。 And then I want to test the variable of the cookie in the View. 然后,我想在View中测试Cookie的变量。
Global.asax.cs : Global.asax.cs:
HttpCookie instock = new HttpCookie("instockV");
instock.Value = "1";
Response.Cookies.Add(instock);
In my view, I'm using jquery : 在我看来,我正在使用jquery:
<div id="test"></div>
$("#test").text($.cookie("instockV"));
But the result in my div is null
. 但是我的div中的结果为null
。
Anyone know about this issue please share. 任何人都知道此问题,请分享。
Thanks. 谢谢。
Did you add the plugin of jquery cookie? 您是否添加了jQuery cookie插件? never the less if you don't have it, to read the cookie the code it's with pure javascript it's easy do it like this 如果您没有它,就更是如此,要使用纯JavaScript读取cookie的代码,就很容易做到这一点
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
UPDATE 更新
Tried in you Global.asax the append like this 在Global.asax中尝试了这种附件
HttpCookie instock = new HttpCookie("instockV");
instock.Value = "1";
Response.AppendCookie(instock);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.