views:

26

answers:

2

EDIT : I have another problem..Now I want to get the cookies value...in controllers constructor...does not allow me to do that since HttpRequest is Null...I dont want to do it on every controller action...since want to do it only once...and then every action can access cookie value

ORIGINAL : Hi all I want to create a cookie for the each of the users of my my asp.net mvc 2.0 application. So I tried creating in Application_Start() of the global.asax. But it does not have access to Response object. Actually there is not a common or index page that will be hit all the time and hence I am not able to do it on some landing page/view. Can anyone suggest me where I can create a cookie?

+2  A: 

You should be able to use the Application_BeginRequest event in your global.asax, this event is triggered when any page is requested.

ryudice
Hey thanks that works though I have another problem..Now I want to get the cookies value...in controllers constructor...does not allow me to do that since HttpRequest is Null...I dont want to do it on every controller action...since want to do it only once...and then every action can access cookie value..
Misnomer
@VJ - since the cookie will have a different value for each user, why would you want to access it in the controller constructor? A controller instance has *nothing* to do with an individual user or session, and you have no control over when the constructor is called in relation to a particular session or request.
GalacticCowboy
+1  A: 

Regarding your edit - you could extract the cookies value in the OnActionExecuting method, which is called before every action is invoked.

If all your controllers inherit from a base controller, override the OnActionExecuting method there and set a (protected) variable with the cookies value - all of your controller actions could then read it...

JonoW
that helped..:)
Misnomer