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;
You may use Request. Cookies collection to read the cookies. Show activity on this post. HttpContext.
In ASP.Net MVC application, a Cookie is created by sending the Cookie to Browser through Response collection (Response. Cookies) while the Cookie is accessed (read) from the Browser using the Request collection (Request.
Cookies are small files that are created in the web browser's memory (if they're temporary) or on the client's hard drive (if they're permanent). CookiesExample.zip. Cookies are one of the State Management techniques, so that we can store information for later use.
Cookies is a small piece of information stored on the client machine. This file is located on client machines "C:\Document and Settings\Currently_Login user\Cookie" path. Its is used to store user preference information like Username, Password,City and PhoneNo etc on client machines.
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?
Remove the static
part of your method declaration and then use Request.Cookies["userId"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With