MVC3 Controller Variable Availability

I am new to MVC3 and come from the background of Winforms. 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 this variable for each user viewing my application?

The second question is a general question ... but I'm not sure of the right words to ask about it. =) Suppose my web application is located on a server that receives some text over the network and needs to send this text to a specific end user (via AJAX), how can I "find" this user session? I am worried that the goal is to publish text in only one user's browser, if there can be 50 on the server. What is the right way for this?

Thanks!

+7
source share
1 answer

If I have a Controller with a private non-static variable in it, I will have a separate "instance" of this variable for each user viewing 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 sequential requests. The controller lifetime is bound only to the specified HTTP request.

How can I "find" this user session?

ASP.NET tracks user sessions using cookies. Cookies are automatically sent at AJAX requests so that the server can identify the user. Take a look at ASP.NET Session State .

+7
source

All Articles