如何在MVC4中恢复会话? [英] How to recover the session in MVC4?

查看:43
本文介绍了如何在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 (< span class =code-keyword> 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中恢复会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆