欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET MVC在基控制器中處理Session

 更新時間:2022年08月13日 08:39:00   作者:Darren Ji  
這篇文章介紹了ASP.NET MVC在基控制器中處理Session的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

當(dāng)需要跨頁面共享信息的時候,Session是首當(dāng)其沖的選擇,最典型的例子就是:在處理登錄和購物車邏輯的時候需要用到Session。在MVC中,可以把處理Session的邏輯放在一個泛型基控制器中,但需要注意的是:在判斷沒有登錄就跳轉(zhuǎn)到登錄頁的時候,需要把出錯控制器和登錄控制器排除在外。

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 string LoginAction = "Login";

        //沒有登錄的跳轉(zhuǎn)到登錄頁
        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            //如果沒有登錄,且不是出錯和登錄控制器就跳轉(zhuǎn)到登錄頁
            if (!NoNeedSessionController(requestContext) && !HasLoginSession())
            {
                GoToAction(requestContext, Url.Action(LoginAction, LoginController));
            }
        }

        //對哪些不需要依賴緩存的控制器 返回true
        private bool NoNeedSessionController(RequestContext requestContext)
        {
            //從路由數(shù)據(jù)中取到當(dāng)前controller的名稱
            var c = requestContext.RouteData.Values["controller"].ToString().ToLower();

            //把不需要依賴Session的控制器名稱放到列表中
            var noNeedSessionList = new List<string>
            {
                errorController.ToLower(),
                LoginController.ToLower()
            };

            return noNeedSessionList.Contains(c);
        }

        //跳轉(zhuǎn)到某個視圖
        private void GoToAction(RequestContext requestContext, string action)
        {
            requestContext.HttpContext.Response.Clear();
            requestContext.HttpContext.Response.Redirect(action);
            requestContext.HttpContext.Response.End();
        }

        //登錄的時候判斷是否有Session
        protected bool HasLoginSession()
        {
            return Session[loginSession] != null;
        }

        //判斷購物車是否有Session
        protected bool HasShoppingCartSession()
        {
            return Session[shoppingCartSession] != null;
        }

        //從Session中獲取登錄模型的實例
        protected TModel GetLoginModelFromSession()
        {
            return (TModel)this.Session[loginSession];
        }

        //從Session中獲取購物車模型的實例
        protected TModel GetShoppingCartModelFromSession()
        {
            return (TModel)this.Session[shoppingCartSession];
        }

        //設(shè)置登錄Session
        protected void SetLoginSession(TModel loginModel)
        {
            Session[loginSession] = loginModel;
        }

        //設(shè)置購物車Session
        protected void SetShoppingCartSession(TModel shoppingCartModel)
        {
            Session[shoppingCartSession] = shoppingCartModel;
        }

        //讓登錄Session失效
        protected void AbandonLoginSession()
        {
            if (HasLoginSession())
            {
                Session.Abandon();
            }
        }

        //讓購物車Session失效
        protected void AbandonShoppingCartSession()
        {
            if (HasShoppingCartSession())
            {
                Session.Abandon();
            }
        }
    }
}

讓其他控制器派生于基控制器:

using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class LoginController : BaseController<LoginModel>
    {
        public ActionResult Index()
        {
            //把登錄模型實例保存到Session中
            LoginModel loginModel = new LoginModel();
            SetLoginSession(loginModel);

            //從Session中獲取登錄模型實例
            LoginModel sessioModel = GetLoginModelFromSession();

            //使登錄Session失效
            AbandonLoginSession();
            return View();
        }

    }
}

到此這篇關(guān)于ASP.NET MVC在基控制器中處理Session的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論