如何在MVC4中恢复会话?

编程入门 行业动态 更新时间:2024-10-26 23:37:09
本文介绍了如何在MVC4中恢复会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好,早上好。我被困在一个应用程序中,我需要恢复丢失的会话。如果人正在进行测试,就像测量中心一样,面对停电问题,他不应该失去会话状态,即当电源恢复时,他的测试也应该恢复。请帮帮我。 最好的问候,

解决方案

要实现这一点,我曾经使用过我遵循的approch如下所示创建一个BaseController类并使用此BaseController继承所有控制器。 在你的BaseController中,覆盖onActionExecuting方法如下

public class BaseController:Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { string 值 = cookie // 为您指定cookie值。 base .OnActionExecuting(fil terContext); string controllerName = filterContext.Controller.GetType()。Name; string actionName = filterContext.ActionDescriptor.ActionName; if ( string .IsNullOrEmpty( value )) { if (!controllerName.Equals( typeof (LoginController).Name)) filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary { { Controller, 登录}, { Action, 索引} }); } else { if ( string .IsNullOrEmpty(Session [ UserId ]) { // 从cookie重新启动会话的代码 } } }

Hi there, good morning. I am stuck in an application where I need to recover the lost session. If person is undergoing a test,just like the prometric center, faced the power-cut issue,he should not lose the session state that is , when the power comes back, his test should also be recovered. please help me on this. best regards,

解决方案

To achive this I had use cookies.The approch I follow is below Create a BaseController Class and inherit all your controller with this BaseController. In your BaseController ,Override onActionExecuting Method like below

public class BaseController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { string value =cookie //assign you cookie value. base.OnActionExecuting(filterContext); string controllerName = filterContext.Controller.GetType().Name; string actionName = filterContext.ActionDescriptor.ActionName; if (string.IsNullOrEmpty(value)) { if (!controllerName.Equals(typeof(LoginController).Name)) filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary { { "Controller", "Login" }, { "Action", "Index" } }); } else { if (string.IsNullOrEmpty(Session["UserId"]) { //code to re-initilize your session from cookies } } }

更多推荐

如何在MVC4中恢复会话?

本文发布于:2023-10-11 22:20:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483004.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!