views:

56

answers:

2

I'm trying to do a “get” of a page, and pass in a list of ids that is stored in my cookie. My assumption was that it would be possible to get the cookie with the list from my request. However, I don’t get the cookie on the server side as I would expect. I only get a “user” cookie, but that doesn’t contain my info.

I want to try to avoid actually passing these values in the URL as I'm trying to keep as clean and nice URL's as possible. However, if it isn't possible in any other way, then I can still do it.

Adding the cookie:

function setCookie(name, value) {
    var today = new Date();
    today.setTime(today.getTime());
    var expires = 30 * 1000 * 60;

    var c_name = name; 
    var exdate = new Date(today.getTime() + (expires));
    document.cookie = c_name + "=" + value + ";expires=" + exdate.toGMTString();
}

Getting the new page in jQuery:

document.location.href = href;

In my Action Method:

if (Request.Cookies != null)
{
    var myCookie = Request.Cookies[cookieName];
}
//myCookie is always null

Shouldn't a cookie I add, still be accessible in the Request?

Edit:

When I've now used FireBug and FireCookie, I can see that the cookie is not passed with the request. So the question is if I do something wrong when adding the cookie?

A: 

Hmmm. what about creating the cookie in the server side by calling a handler?

// on CookieHandler.ashx
HttpCookie myCookie = new HttpCookie("Your Data");
Response.Cookies.Add(myCookie);

and in your javascript method you can invoke your cookie handler by using XmlHttpRequest object or JSON call.

         function setCookie(name, value){
         var cookieQuery= 'AppCode/cookieHandler.ashx?name='+name+'&value='+value;
             var xmlhttp;
             if (window.XMLHttpRequest) {
             // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
             }
             else
             if (window.ActiveXObject) {
             // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
             }
             else {
                alert("Your browser does not support XMLHTTP!");
             }
             xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                   // Do Something
                   //alert(xmlhttp.responseText);
                }
             }

             xmlhttp.open("GET", cookieQuery, true);
             xmlhttp.send(null); // Sends the request to the server.

          }  

Hope this helps.

Juan Zamora M
wouldn't the same data be available to him in the Controller as would be available in the .ashx file?
DaveDev
A: 

The reason why I couldn't access the cookie was because of the cookie path. When I was looking thru FireCookie, the path was the only thing not matching with the cookie that was accessible in the request.

The one that was accessible was looking like this:

Path: "/"

and the one that I wanted to access, but was unable to, was looking like this:

Path: "/{projectName}/{controller}/{action}"

So when I'm now saving my cookie, I changed it from being this:

document.cookie = c_name + "=" + value + ";expires=" + exdate.toGMTString();

To be this instead:

document.cookie = c_name + "=" + value + ";Path=/;expires=" + exdate.toGMTString();

And it all works out fine then.

MrW