MVC implements Session processing logic in the base Controller

Source: Internet
Author: User

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

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.