Developer forum

Forum » Development » Access Session State with Razor?

Access Session State with Razor?

Dmitrij Jazel
Reply

Hello DW Guys,

Wanted to ask you if it is possible to access Session state in DynamicWeb templates with Razor?

I wanted to store a single string value as string. And use it from time to time if I need it.

Problem is that I need to keep it as long as whole session is active.

Or maybe there are some alternatives I can use?

 

Currently I am trying to use something like this:

HttpContext.Current.Session["whatever"] = "mystring";

But Razor compains :-(

RazorEngine.Templating.TemplateCompilationException: Unable to compile template. The name Http Context does not exist in the current context

Adding @using namespace statement does not help neither :-(

 

Any suggestions?

Regards,

Dmitrij

 


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

Did you prefix HttpContext with System.Web? The following works for me:

 

@{
  System.Web.HttpContext.Current.Session["whatever"] = "mystring";
}

@System.Web.HttpContext.Current.Session["whatever"]

 

 

Cheers,

 

Imar

Votes for this answer: 1
 
Dmitrij Jazel
Reply

Hej Imar,

Nice :) it works now!

Thanks allot for help! :)))

 

Dmitrij

 

 
Mikkel Ricky
Reply

Just to follow up on a @using namespace statement, this should also work:

@using System.Web;

@{
	HttpContext.Current.Session["whatever"] = "mystring";
}

@HttpContext.Current.Session["whatever"]

I've tested successfully on Dynamicweb 8.2.3.2.

Best regards,
Mikkel

 
Dmitrij Jazel
Reply

Hej Mikkel :)

Thanks allot for the tip! :)

Works great aswell :)

Regards,

Dmitrij

 

You must be logged in to post in the forum