Khoa Nguyen
Khoa Nguyen

Reputation: 55

Session variable is reset after making a request to Web API controller

I'm using Session in Web API by implementing follow the answer Enable session in Web Api 2. But I got a problem with Session variables.

In business object class, I have a property which is used to store the changed record.

public class BaseBO
{
  protected HttpSessionState _session;

  public BaseBO()
  {
      _session = HttpContext.Current.Session;
  }

  private IList<FundInvestor> _investors;
  public  IList<FundInvestor> Investors
  {
    get
    {
        _investors = (IList<FundInvestor>)_session["_investors"];
        if (_investors == null)
        {
            _investors = new List<FundInvestor>();
            _session["_investors"] = _investors;
        }
        return _investors;
    }
    set
    {
        _investors = value;
        _session["_investors"] = value;
    }
  } 
}

At client, when saving a form, I make two requests to server. First, I call method AddInvestorQueue of a BaseBO instance with the list of changed records. The changed records will be added into list Investors of BaseBO. Second request, I call method Edit in which I will get the element of the list Investor and update these records to database.

My problem is that the list Investors doesn't have any element when I call the second request. Note that there were records added into Investors in the first request. I don't know why that list is reset.

This issue doesn't occurs when I use ASP.NET Web Form.

Is there anyone met the similar issue? I get stuck with this issue. Any advice is very helpful for me. Thank very much.

Upvotes: 2

Views: 954

Answers (1)

tehila
tehila

Reputation: 144

session in web api is stateless. I would offer you use token based authentication, you can see at https://github.com/tb1998tb/token (web api + angular). goodluck!

Upvotes: 1

Related Questions