Session values cant be obtained between two different web api calls

I have two Web API methods called in different cases. I have used the SetString method and the value is set in method 1 but when the 2nd method is called for other cases, accessing the value using the GetString method it returns the value as null.

public string Method1(string args)
   HttpContext.Session.SetString("key", "value"); // value is set in this method.

public string Method2(string args)
   string value = HttpContext.Session.GetString("key"); // value is obtained as null here

Any suggestions to resolve this issue. I have added controller manually in the blazor server-side application for WebAPI call.

1 answer

  • answered 2021-01-17 13:42 Roar S.

    Please make sure the following exists in Startup:


    services.AddSession(options => {
       options.IdleTimeout = System.TimeSpan.FromSeconds(120); // two minutes



    Please check here for ordering:

    Important If you run your app on multiple instances, you'll need to choose a strategy. Storing sessions using Distributed Cache is preferable. More info:

    Another way of solving multiple server issue, is to use sticky sessions on your load balancer. This is not recommended because of the risc of uneven spread of load.

    If you are able to get rid of the need for sessions in your app, I would highly recommend that.