I need to retrieve the data from cookie in every request in ASP.NET MVC and store it in a global variable so that it’ll be available throughout the application.
I’ve two questions here is there any event-handler in ASP.NET MVC where I can get the data from cookie in every request and what kind of global variable I can use to store this cookie value so it is available in all places?
You can use a filter to get the cookie in every request. Create for example a class
MyFilterMark every controller class with
[MyFilter]attribute.This example takes the cookie and puts the value in the session so it’s available in all the views and all the controllers. Else you can put the cookie value in a static class that contains a static
Dictionaryand use the session ID as key. There are many way to store the cookie value and access it in every part of the application.