本文介绍了如何在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中恢复会话?
发布评论