Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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) ?
Posted
Updated 13-Aug-13 2:25am
v2

1 solution

SQL
if (!string.IsNullOrEmpty(Session["value1"] as string) && !string.IsNullOrEmpty(Session["value2"] as string))
{

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900