Programming in C#: Why is Request.Cookies null? on newest questions tagged c# – Stack Overflow

This is a follow-up on another thread I have running which is Reading cookie in c# . However, the answers in that post seem to beat around the bush so I want to simplify my question here. I think that answering this, I’ll be able to solve that problem. Stuck almost two days now trying to read a cookie so any help is appreciated.

What could cause a NullReferenceException at this line:

HttpCookie aCookie = Request.Cookies["UserSettings"];

I am sure a cookie called “UserSettings” is there, as I can see it with developer toolbar.
So Request or Request.Cookies must be null, right?

Why can’t I use Request.Cookies? Every single cookie tutorial I look at does it like that.

EDIT: added cookie creation code in index.aspx, btw im trying to read the cookie in HomeController.cs, dont know if this matters, but thought id mention it.

<script type="text/javascript">
    function setLanguage() {
    cname = "language";
    cvalue =           document.getElementById('language').options[document.getElementById('language').selectedInd    ex].value;
    cexpire = new Date();
    cexpire.addDays(1);
    document.cookie = cname + '=' + escape(cvalue) +
(typeof cexpire == 'date' ? 'expires=' + cexpire.toGMTString() : '') +
',path=/;';
}
</script>

See Answers


source: http://stackoverflow.com/questions/11031243/why-is-request-cookies-null
Programming in C#: programming-in-c



online applications demo