最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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);
            //如果沒有登錄,且不是出錯(cuò)和登錄控制器就跳轉(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)到某個(gè)視圖
        private void GoToAction(RequestContext requestContext, string action)
        {
            requestContext.HttpContext.Response.Clear();
            requestContext.HttpContext.Response.Redirect(action);
            requestContext.HttpContext.Response.End();
        }

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

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

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

        //從Session中獲取購物車模型的實(shí)例
        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()
        {
            //把登錄模型實(shí)例保存到Session中
            LoginModel loginModel = new LoginModel();
            SetLoginSession(loginModel);

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

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

    }
}

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

相關(guān)文章

最新評論

城步| 兰西县| 沅江市| 博乐市| 固阳县| 阿拉尔市| 普定县| 舟曲县| 济源市| 扬中市| 枣阳市| 台南县| 临安市| 永安市| 吴川市| 手机| 泰兴市| 临清市| 县级市| 福贡县| 莱西市| 绿春县| 西乌珠穆沁旗| 金阳县| 石泉县| 朔州市| 铜山县| 白山市| 兴城市| 汉阴县| 南溪县| 蚌埠市| 衢州市| 绥化市| 姚安县| 加查县| 金堂县| 高淳县| 亚东县| 宝应县| 岑溪市|