網站首頁 編程語言 正文
當需要跨頁面共享信息的時候,Session是首當其沖的選擇,最典型的例子就是:在處理登錄和購物車邏輯的時候需要用到Session。在MVC中,可以把處理Session的邏輯放在一個泛型基控制器中,但需要注意的是:在判斷沒有登錄就跳轉到登錄頁的時候,需要把出錯控制器和登錄控制器排除在外。
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";
//沒有登錄的跳轉到登錄頁
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
//如果沒有登錄,且不是出錯和登錄控制器就跳轉到登錄頁
if (!NoNeedSessionController(requestContext) && !HasLoginSession())
{
GoToAction(requestContext, Url.Action(LoginAction, LoginController));
}
}
//對哪些不需要依賴緩存的控制器 返回true
private bool NoNeedSessionController(RequestContext requestContext)
{
//從路由數據中取到當前controller的名稱
var c = requestContext.RouteData.Values["controller"].ToString().ToLower();
//把不需要依賴Session的控制器名稱放到列表中
var noNeedSessionList = new List<string>
{
errorController.ToLower(),
LoginController.ToLower()
};
return noNeedSessionList.Contains(c);
}
//跳轉到某個視圖
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];
}
//設置登錄Session
protected void SetLoginSession(TModel loginModel)
{
Session[loginSession] = loginModel;
}
//設置購物車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();
}
}
}
原文鏈接:https://www.cnblogs.com/darrenji/p/3819225.html
相關推薦
- 2022-07-21 Gitee:使用ssh提交代碼卻提示:DeployKey does not support push
- 2022-11-22 Android?10?啟動之servicemanager源碼解析_Android
- 2022-12-12 Android?DataBinding類關系深入探究_Android
- 2023-01-26 Kotlin協程Channel源碼示例淺析_Android
- 2023-10-17 將一維數組截取成二維數組,要求二維數組中的每個數組length是一樣的才可以
- 2022-04-02 docker建立私有倉庫的過程_docker
- 2022-07-04 PyTorch計算損失函數對模型參數的Hessian矩陣示例_python
- 2022-10-14 linux服務器安裝redis并設置redis開機自啟和遠程連接
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支