如何在MVC4中恢复会话? [英] How to recover the session in MVC4?
本文介绍了如何在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屋!
查看全文