当需要跨页面共享信息的时候,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();
} }
}

最新文章

  1. zabbix监控Java 8080端口
  2. php实现设计模式之 桥接模式
  3. 功能更新到 Windows 10 企业版, 版本 1607
  4. 使用exe4j打包Java程序
  5. 协程python
  6. php大力力 [037节] Iconfont-阿里巴巴矢量图标库
  7. wangEditor——轻量化web富文本框
  8. 解决tomcat默认45s启动超时的问题
  9. IPython notebook 使用介绍
  10. 修改Windows XP的桌面路径
  11. linux下创建用户并且限定用户主目录
  12. Json与Java对象互转之Gson学习
  13. 实现app上对csdn的文章列表上拉刷新下拉加载以及加入缓存文章列表的功能 (制作csdn app 四)
  14. iOS-MD5加密、SHA1加密
  15. rocketmq有序消息
  16. 如何理解Python装饰器
  17. react native 0.50与OC交互 &amp;&amp; Swift与RN交互
  18. Oracle nal() 和count(*)的注意点
  19. [python]上传文件验证
  20. Android Dialog.dismiss()与Activity.finish()顺序

热门文章

  1. CVE-2013-1347Microsoft Internet Explorer 8 远程执行代码漏洞
  2. CF 576A 猜数
  3. HP 打印机监控
  4. JavaScript工程师都应懂的33个概念
  5. Bzoj1018/洛谷P4246 [SHOI2008]堵塞的交通(线段树分治+并查集)
  6. [漏洞复现]CVE-2010-2883 Adobe Reader 打开pdf电脑即刻中招
  7. Expression表达式树 案例
  8. 批量ping工具fping
  9. Django框架(一)-Django初识
  10. 【HackerRank Week of Code 31】Colliding Circles