When you need to share information across pages, Session is the first choice. The most typical example is to use Session when processing login and shopping cart logic. In MVC, you can put the Session processing logic in a generic base controller. However, note that when the logon page is displayed when no logon is detected, the error controller and logon controller must be excluded.
Using System. collections. generic; using System. web. mvc; using System. web. routing; namespace MvcApplication1.Controllers {public class BaseController <TModel>: Controller {private const string loginSession = "LoginSession"; private const string shoppingCartSession = "ShoppingCartSession "; private const string errorController = "Error"; private const string LoginController = "Login"; private const stri Ng LoginAction = "Login"; // if you have not logged on, jump to the logon page protected override void Initialize (RequestContext requestContext) {base. initialize (requestContext); // if you have not logged on, and do not encounter errors or log on to the Controller, go to the logon page if (! NoNeedSessionController (requestContext )&&! HasLoginSession () {GoToAction (requestContext, Url. action (LoginAction, LoginController) ;}// returns true private bool NoNeedSessionController (RequestContext requestContext) for controllers that do not need to be cached) {// obtain the name var c = requestContext of the current controller from the route data. routeData. values ["controller"]. toString (). toLower (); // put the Controller name that does not depend on the Session in the List var noNeedSessionList = new List <string> {errorController. toLower (), LoginControll Er. toLower ()}; return noNeedSessionList. contains (c);} // jump to a view private void GoToAction (RequestContext requestContext, string action) {requestContext. httpContext. response. clear (); requestContext. httpContext. response. redirect (action); requestContext. httpContext. response. end () ;}// determine whether a Session protected bool HasLoginSession () {return Session [loginSession] exists during logon. = Null;} // determines whether the shopping cart has a Session protected bool HasShoppingCartSession () {return Session [shoppingCartSession]! = Null;} // obtain the instance protected TModel GetLoginModelFromSession () {return (TModel) this from the Session. session [loginSession];} // obtain the instance protected TModel GetShoppingCartModelFromSession () {return (TModel) this from the Session. session [shoppingCartSession];} // set the logon Session protected void SetLoginSession (TModel loginModel) {Session [loginSession] = loginModel;} // sets the shopping carsession protected void callback (TModel shoppingCartModel) {Session [shoppingCartSession] = shoppingCartModel;} // invalidate the logon Session protected void AbandonLoginSession () {if (HasLoginSession () {Session. abandon () ;}}// invalidate the cart Session protected void AbandonShoppingCartSession () {if (HasShoppingCartSession () {Session. abandon ();}}}}
Let other controllers derive from the base controller:
Using System. web. mvc; using MvcApplication1.Models; namespace MvcApplication1.Controllers {public class LoginController: BaseController <LoginModel> {public ActionResult Index () {// Save the login model instance to the Session LoginModel loginModel = new LoginModel (); SetLoginSession (loginModel); // obtain the login model instance LoginModel sessioModel = GetLoginModelFromSession () from the Session (); // invalidate the logon Session AbandonLoginSession (); return View ();}}}