- An actionlink is just a helper method to create an <a href="... tag. You pass it the controller and action you want, and it uses your routing table to determine how to build the href attribute.
- To do what you want, your controller will need to change the session variable.
So, if the fact that a controller has been called is enough to figure out what the new session variable should be, then that's easy -- simply change the session variable in the action.
However, it might be that you want different links to the same action to set different session variables. In that case, your actinlink might look something like this:
<%= Html.ActionLink("Click Me", "action_name", "controller", new {session="new_value"}) %>
which causes something like this (depending on your route table):
<a href="/controller/action_name?session=new_value">Click Me</a>
So now you have to set your session variable in your action:
public ActionResult action_name(string session)
{
Session["val"] = session;
....
EDIT:
Considering your comment, there are two ways to go about it: an attribute decorating controllers or a master controller. I'd recommend the master controller as it'd probably be easiest for you to implement.
Since you didn't include any specific examples, I'll make one up. It's where, regardless of which action is called, a particular querystring equates to a particular value in the session, like for setting language.
I'm going from memory here, so you might have to experiment a bit (especially with where exactly the session and request objects are). If not, I can look at my code later. If you don't already have one, create a master controller that your other controllers derive from. Override initialize (calling base.Initialize within it). And then take a look at the querystrings, like:
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
if (requestContext.Current.Request["language"])
requestContext.Current.Session["language"] = HttpContext.Current.Request["language"];
}
James