Users and Cookies in ASP.NET MVC
-
Hi, The situation is this: I would like to have an static method in some class of the Models library like this static User GetCurrentUser() Which takes a cookie in the user ("currentUserId") and returns the User retrieved from the db.. The problem: I cant have access to the cookies (to the "Request" class) from the Model lib. So what other solution can I use? Should I make that method in the Controllers library? IF i dont add a view to a method of a controller class then that method wont be an action? ALSO: The method needs to be used from other controllers.. so.. is that possible?
-
Hi, The situation is this: I would like to have an static method in some class of the Models library like this static User GetCurrentUser() Which takes a cookie in the user ("currentUserId") and returns the User retrieved from the db.. The problem: I cant have access to the cookies (to the "Request" class) from the Model lib. So what other solution can I use? Should I make that method in the Controllers library? IF i dont add a view to a method of a controller class then that method wont be an action? ALSO: The method needs to be used from other controllers.. so.. is that possible?
Why dont you use Static Property...
HttpContext.Current.Request.Cookies
This will do the trick... :rose:Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
-
Hi, The situation is this: I would like to have an static method in some class of the Models library like this static User GetCurrentUser() Which takes a cookie in the user ("currentUserId") and returns the User retrieved from the db.. The problem: I cant have access to the cookies (to the "Request" class) from the Model lib. So what other solution can I use? Should I make that method in the Controllers library? IF i dont add a view to a method of a controller class then that method wont be an action? ALSO: The method needs to be used from other controllers.. so.. is that possible?
Separation of concerns is a reason to use the MVC pattern. By trying to use the Request object in your model you are violating the tenets of MVC. There is not a one-to-one correlation between views and actions. You can have an action that redirects to another view or renders another view. Create a BL class that has the GetCurrentUser method and call it from the controllers as necessary.
only two letters away from being an asset
-
Why dont you use Static Property...
HttpContext.Current.Request.Cookies
This will do the trick... :rose:Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
As a basic tenet of the MVC pattern the model should not need to know about such things.
only two letters away from being an asset