Question

I'm saving a cookie in a C#.NET MVC View, like so

$.cookie("test", testvalue);

But when I try to retrieve that value from the C#.NET controller, I get a null value, and what's more, the cookie key is not in the Cookies collection even.

var test = Request.Cookies["test"];

When I check with Mozilla/Firebug, I see that the cookie is there all right. For some reason the C#.NET Cookies collection is unable to fetch it.

Any ideas?

Was it helpful?

Solution

I think you should use path

$.cookie("test", 1, { expires : 10, path : '/', domain : 'jquery.com',secure : true});

or you can create and fetch cookie simply by javascript

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top