MVC implements the logic of session processing in the base controller

Source: Internet
Author: User

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 (); }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.