I have an MVC application, and I need to create and store a unique session Id for each application instance. In standard ASP.NET this is easy, I would have simply added a hidden field in the master page and stored a Guid there on the first Page_Load.
However, there is not code behind in MVC, and I also believe that you can't implement a controller for a master page OR create a strongly typed view master page.
Anyone know an easy way around this? I'm just playing around with MVC for the first time so be gentle if what I'm asking seems stupidly simple.
Thanks
If you need to persist a few objects for the lifetime of an instance of your application I would recommend you using the built-in Cache object or Application. If those objects need to be specific to each user then use the Session object.
Why not store this in a cookie?
Related
All:
I have been searching high and low for an answer to this, so forgive me if this is a dupe, I just can't seem to find the right answer.
Let's say you have an ASP.NET MVC Controller marked with the [SessionState(SessionStateBehavior.Disabled)]
attribute. Does calling actions on this controller "refresh" the session state, keeping it "alive"? Specifically, I have a AJAX request calling a controller to keep the session "alive" since the application is a single page application driven by javascript, and I don't want the users session to die, so every 30 seconds I make a call up to this controller. Similarly, would it stay alive if the controller was marked SessionStateBehavior.ReadOnly? Finally, is using an ASP.NET MVC Controller for this purpose not the best way (is there a better way)?
Thanks!
Session state will be kept alive automatically with no attributes on the action, depending on how the web.config is configured. To configure it, see here: http://msdn.microsoft.com/en-us/library/h6bb9cz9(v=vs.100).aspx
A controller should be okay, but you may want to look into Web API as it'll offer a few more features in this scenario: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
In the app I'm working on the administrator has the ability to select a customer to work with in a drop down list. Thing is that there are a large number of views that the administrator can go through and each time they'd have to select the customer again. In webforms this was rather easy... store it in a Session variable and reset it when another page loads.
MVC not so much. I seem to be stuck at the point where I pass this value to the view from the controller. We are storing the value in a Session variable which we access using a base controller like this:
MyController.CurrentUser.CurrentCustomerId
The question I can't solve is how I pass this value to a partial view. The customer selector tool is in a partial view which is added to pages that need it.
I thought of using the ViewBag, but that means that in every single action in my controllers that requires this value I would have to add:
ViewBag.CurrentCustomerId = CurrentUser.CurrentCustomerId;
And even then I'm not sure if the ViewBag data is carried through to the partial view. I think it is.
Seems like there should be a more efficient way to do this and still abide by MVC rules?
#Andre Calil provided a very good solution for carrying user information between controllers and views and I ended up using this approach.
See his answer at the following link: Razor MVC, where to put global variables that's accessible across master page, partiview and view?
How can I keep some values between different Views in MVC 3?
I tried using TempData and HiddenField to keep these values but in our hosting this tecnique seems to have short life so exceptions are coming out furthermore if user uses Back button every starts to fail.
I would like to understand the better way to keep values between views in MVC 3, thanks in advice!
By design, MVC3 TempData values are removed after they are used.
The most simple answer is to use the Session object directly in your controllers.
There are other related questions with detailed answers such as these:
Session variables in ASP.NET MVC
Asp.Net MVC and Session
Your question is about the lifecycle of objects in between requests. It's important to understand that webapplications are used over the HTTP(S) protocol which is a stateless protocol. This means that every request is a completely new request for the webserver and there's no state shared between requests.
However it would be foolish to send the credentials of a user to the server each and every time so a webserver can create a thing they call a Session (and session-state). This object is an object that remains available for the lifetime of the session of the current user (most of the times from logging in until logging out). You can use this object to store items that you wish to share over various requests of the same user.
If the values you're trying to keep are specific to the page you can probably use a hidden field or something like that. However if the data is more related to the user than to a specific page and it must have a lifecycle longer than a single request then sessionstate is the best place to store the data.
You could use the Session (as you mention in your title and tags). Or store a cookie on the user's machine
I am developing a website using asp.net MVC
I want to show the name of user after he/she logged in the website.
The problem is that User.Identity.Name returns user email and I do not want to change that because user name is not unique. And I want to access email in other pages.
I like to use a viewBag in my master page, but I do not know where to define it.
If I define it in Home/controller, it works just for this action.
Should I use filter?
This link will help you with the problem of passing data to master page.
This is the "official" way to handle that, it explains in a very clear way, but I strongly recommend to read the first link completly too.
If you're using MVC, then I would strongly suggest moving to a ViewModel pattern to accomplish this, rather than stuffing objects into the ViewBag, mostly for two reasons: 1) Type Safety, and 2) Intellisense support.
In this kind of situation, you would have your MasterPage inherit from a BaseViewModel, and your actions would return derived objects from the BaseViewModel. You can then set data on your view model, and it will be available to the master page when rendering:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MyViewModelBase>" %>
Alternatively, you could make a 'partial view' that renders just the content you need based on the user's state.
Just getting started using MVC in ASP.NET, I'm going to have it so users must login to use certain features. Now I have a User controller that stores users in a table and another controller that adds data to another table. Once the user is logged in, how would I get their id from the user table from within the add controller in order to add their id to that table?
I think that to solve your problem from the top down you might want to look into ASP.NET MVC Authentication instead of implementing something like this yourself. That said if you have a great reason for continuing down the path you're taking then I have some suggestions.
Firstly you may wish to consider using the repository pattern to add/remove/get data to and from your database. Any controller can implement any repository it likes so your add controller can just implement the user repository to get the user.
Also, remember that in ASP.NET MVC you can use session variables. If you need to know which user is doing what, then just store it in the session and retrieve it from there.