MVC会话到期:检查基本控制器中的会话是否为空 [英] Expiring of MVC Session : Checking if Session is Null in Base Controller
问题描述
我在控制器的索引方法中创建一个会话(继承基本控制器)。
//这段代码是控制器的索引方法
int maxValue = 100; //此处硬编码此值为现在..
会话[MySession] = maxValue;
我正在使用此会话使用Ajax调用进行一些验证
[HttpPost]
public ActionResult IsLessThanMaxValue(int val )
{
if(Session [MySession] as int> val)
返回Json(false);
其他
返回Json(true);
}
因为我我在页面的每个事件上使用此会话(模糊,通过Ajax调用更改)我必须先确定,是否为空。我正在尝试它,在基本控制器中执行
以下功能:
protected override void OnActionExecuting
在基本控制器中我想检查这个会话是否为空
如果确实...我需要它来重定向它到其他页面。
我试过以下解决方案:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
//如果会话存在
if(filterContext.HttpContext.Session!= null)
{
//如果是新会话
if(filterContext.HttpContext.Session.IsNewSession)
{
string cookie = filterContext.HttpContext.Request.Headers [Cookie];
//如果cookie存在且sessionid索引大于零
if((cookie!= null)&&(cookie.IndexOf(ASP.NET_SessionId)> = 0))
{
//重定向到所需的会话
//到期行动和控制器
filterContext.Result = this.Redirect(Some url);
return;
}
}
}
//否则继续行动
base.OnActionExecuting(filterContext) ;
}
但它不起作用。看来如下条件
(cookie.IndexOf(ASP.NET_SessionId)> = 0)需要更改为
(cookie.IndexOf(ASP.NET_SessionId)< 0)
所以这里是我的问题
1)我是否需要扭转上述条件?
2)有没有更好的方法来做到这一点? (我无法在基本控制器中添加会话的空检查,因为当第一次加载页面时,它将保持为空并且我不希望我的页面仅重定向OnLoad)?
I am creating a session in index method of a controller(inheriting base controller).
// this code in is index method of the controller
int maxValue= 100 ;// Hard coded this value here as of now..
Session["MySession"] = maxValue;
I am using this session to make some validations using Ajax call
[HttpPost]
public ActionResult IsLessThanMaxValue(int val)
{
if(Session["MySession"] as int > val)
return Json("false");
else
return Json("true");
}
Since I am using this session on every event of the page (blur , change via Ajax call) I have to determine first, whether its empty or not. I am trying it, to do in Base Controller
in the following function :
protected override void OnActionExecuting
In base controller I want to check if this session is null
If it does ... I need to it to redirect it to some other page.
I tried following solution :
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
// If session exists
if (filterContext.HttpContext.Session != null)
{
//if new session
if (filterContext.HttpContext.Session.IsNewSession)
{
string cookie = filterContext.HttpContext.Request.Headers["Cookie"];
//if cookie exists and sessionid index is greater than zero
if ((cookie != null) && (cookie.IndexOf("ASP.NET_SessionId") >= 0))
{
//redirect to desired session
//expiration action and controller
filterContext.Result = this.Redirect("Some Url");
return;
}
}
}
//otherwise continue with action
base.OnActionExecuting(filterContext);
}
But it doesn't work. It seems the following condition
(cookie.IndexOf("ASP.NET_SessionId") >= 0) needs to be changed as
(cookie.IndexOf("ASP.NET_SessionId")< 0)
So here are my questions
1) Do I need to reverse the mentioned condition ?
2) Is there a better approach to do this ? (I cannot add null check of session in Base Controller since when the page loads for the first time , it will remain as Null and i don't want my page to redirect OnLoad only) ?
推荐答案
if (!string.IsNullOrEmpty(Session["value1"] as string) && !string.IsNullOrEmpty(Session["value2"] as string))
{
}
这篇关于MVC会话到期:检查基本控制器中的会话是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!