Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Controller Variable Accessibility

I am new to MVC3 and coming from a Winforms background. I have a two-part question. The first is simple - if I have a Controller with a private non-static variable in it, will I have a separate "instance" of that variable for each user that browses to my application?

The second is more of a general question... but I'm not sure of the right words to ask it. =) Let's suppose that my web app sits on a server that is being fed some text via the network, and needs to post that text to a particular end user (via AJAX), how do I "find" that user's session? I'm worried about the goal being to post the text in only one user's browser when there might be 50 connected to the server. What's the right way to go about that?

Thanks!

like image 612
Chris Barlow Avatar asked Oct 05 '11 13:10

Chris Barlow


1 Answers

if I have a Controller with a private non-static variable in it, will I have a separate "instance" of that variable for each user that browses to my application

Yes, you will have a separate instance for each user request. You will have a separate instance of the controller (and the private field) even for the same user if he performs consecutive requests. A controller's lifetime is tied only to a given HTTP request.

how do I "find" that user's session?

ASP.NET tracks user sessions using cookies. Cookies are automatically sent along AJAX requests so the server will be able to identify the user. Take a look at ASP.NET Session state.

like image 152
Darin Dimitrov Avatar answered Oct 02 '22 17:10

Darin Dimitrov