Reference:Session

From Bistro
Jump to: navigation, search

Contents

Overview

The Session attribute indicates that the value of this field is maintained in the Session.

Usage

This attribute is specified for Property and Field level usage. Members marked with this attribute will be populated with the corresponding value from the session prior to controller execution, and will have their value propagated to the session after execution has completed.

This attribute is typically used in conjunction with a Provides/Requires/DependsOn attribute. Fields not marked with any of the three, will be defaulted to Provides.

Note that while not recommended, this attribute can be used in conjunction with the Request attribute. In such a case, the values of the two fields are synchronized, with the value of the Session-level field taking priority over the Request-level field.

Name

The name property can be used to redefine the resource name of the session entry bound to this member. The value defaults to the name of the member marked.

Example

The following code demonstrates how to make a Session value available to the template

[Bind("/action/search/{contentType}")]
public class Search : AbstractController
{
    [Session, DependsOn]
    protected List<string> currentTags;
 
...
}


The following template will then display the session value

...
{% for tag in currentTags %}
    {{ tag }} <a href="{{ root }}/action/search/{{ contenType }}/without-tag/{{tag|urlencode}}"> x</a>
    {% if not forloop.first %}
        ,&nbsp;
    {% endif %}
{% endfor %}
...


This value is then also available to other controllers

[Bind("/action/search/{contentType}/with-tag/{tagList}")]
public class Tag : AbstractController
{
    [Session]
    protected List<string> currentTags;
 
    protected string tagList;
 
    public override void DoProcessRequest(IExecutionContext context)
    {
        if (currentTags == null)
            currentTags = new List<string>();
 
        foreach (string tag in tagList.Split(','))
            if (!currentTags.Contains(tag))
                currentTags.Add(tag);
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox