When it comes to sharing information across pages, the session is the first choice, and the most typical example is the need to use the session when dealing with login and shopping cart logic. In MVC, the logic of the session can be placed in a generic base controller, but it is important to note that the error controller and the login controller need to be excluded when deciding to jump to the login page without logging in.
usingSystem.Collections.Generic;usingSYSTEM.WEB.MVC;usingSystem.Web.Routing;namespacemvcapplication1.controllers{ Public classBasecontroller<tmodel>: Controller {Private Const stringLoginsession ="loginsession"; Private Const stringShoppingcartsession ="shoppingcartsession"; Private Const stringErrorcontroller ="Error"; Private Const stringLogincontroller ="Login"; Private Const stringLoginaction ="Login"; //jump to sign-in page without login protected Override voidInitialize (RequestContext requestcontext) {Base. Initialize (RequestContext); //If you are not logged in, and you are not logged in to the controller, jump to the login page if(! Noneedsessioncontroller (requestcontext) &&!hasloginsession ()) {gotoaction (RequestContext, Url.action (Loginaction, Logincontroller)); } } //returns true for controllers that do not need to rely on caching Private BOOLNoneedsessioncontroller (RequestContext requestcontext) {//take the name of the current controller from the routing data varc = requestcontext.routedata.values["Controller"]. ToString (). ToLower (); //Place controller names that do not need to be dependent on the session in the list varNoneedsessionlist =Newlist<string>{errorcontroller.tolower (), Logincontroller.tolower ()}; returnNoneedsessionlist.contains (c); } //jump to a view Private voidGotoaction (RequestContext RequestContext,stringaction) {requestContext.HttpContext.Response.Clear (); RequestContext.HttpContext.Response.Redirect (action); RequestContext.HttpContext.Response.End (); } //when you log in, determine if there is a session protected BOOLhasloginsession () {returnSession[loginsession]! =NULL; } //determine if the shopping cart has a session protected BOOLhasshoppingcartsession () {returnSession[shoppingcartsession]! =NULL; } //to get an instance of the login model from the session protectedTModel getloginmodelfromsession () {return(TModel) This. Session[loginsession]; } //get an example of a shopping cart model from the session protectedTModel getshoppingcartmodelfromsession () {return(TModel) This. Session[shoppingcartsession]; } //Set Login Session protected voidsetloginsession (TModel loginmodel) {session[loginsession]=Loginmodel; } //set up shopping cart session protected voidsetshoppingcartsession (TModel shoppingcartmodel) {session[shoppingcartsession]=Shoppingcartmodel; } //Disable Login Session protected voidabandonloginsession () {if(Hasloginsession ()) {Session.Abandon (); } } //Disable the shopping cart session protected voidabandonshoppingcartsession () {if(Hasshoppingcartsession ()) {Session.Abandon (); } } }}
To derive the other controller from the base controller:
usingSYSTEM.WEB.MVC;usingMvcapplication1.models;namespacemvcapplication1.controllers{ Public classLogincontroller:basecontroller<loginmodel> { PublicActionResult Index () {//Save the Login model instance to the sessionLoginmodel Loginmodel =NewLoginmodel (); Setloginsession (Loginmodel); //Get the Login model instance from the sessionLoginmodel Sessiomodel =getloginmodelfromsession (); //invalidate the login sessionabandonloginsession (); returnView (); } }}