ANIL MANE
ANIL MANE

Reputation: 1405

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

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;

Upvotes: 30

Views: 78115

Answers (2)

ilivewithian
ilivewithian

Reputation: 19692

Remove the static part of your method declaration and then use Request.Cookies["userId"]

Upvotes: 11

Ian Suttle
Ian Suttle

Reputation: 3402

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?

Upvotes: 41

Related Questions