How do I access Request.cookies in an ASP.NET MVC controller?


Question

I'm trying to get a user ID stored in cookies via a common Controller file, which I can access throughout the site.

I have created FunctionsController as a controller, with content as follows:

public static int loggedinUser()
{
    return Convert.ToInt32(  request.Cookies["userid"].Value);
}

I am unable to request any cookie items even if I tried with:

HttpRequestBase request = controllerContext.HttpContext.Request;
1
26
11/7/2016 7:17:03 PM

I don't have a problem accessing cookies in ASP.NET MVC using a standard access statement such as:

Request.Cookies["someCookie"]

Your sample had a lower-cased "r" in "request.Cookies". Could that be your problem?

31
4/4/2009 6:36:19 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon