Reputation: 2645
I want to save some variables for future reference so I declared a class Container.cs
public class Container
{
public static String verifierCode;
}
and then I have my HomeController.cs
where I update the Container's variables values:
public class HomeController : Controller
{
public ActionResult ValidateTwitterAuth()
{
Container.verifierCode = Request.Query.ElementAt(2).Value;
//do some stuff
}
public String sendTweet(String NewTweet)
{
var userCreds = AuthFlow.CreateCredentialsFromVerifierCode(Container.verifierCode, Container.authorizationId);
var user = Tweetinvi.User.GetAuthenticatedUser(userCreds);
user.PublishTweet(NewTweet);
return "sent!";
}
}
It gets updated in the ValidateTwitterAuth
with the specific values but when the sendTweet
method is called the Container.verifierCode
is null.
I obviously tried to debug it but if I add the variable to quickwatch i get that
error CS0103: The name 'Container' does not exist in current context
Also, both the class and the container are in the same namespace. Any idea/tip why this is happening? I am just trying to declare a global variable which I can access from other classes/controllers.
I apologise if this may sound like a beginner question but I'm just starting to get my hands on asp.net
EDIT: From the answers, my way of handling with the problem was wrong. Therefore I installed the Microsoft.AspNetCore.Session
NuGet Package and used it like this:
HttpContext.Session.SetString("verifierCode", Request.Query.ElementAt(2).Value);
HttpContext.Session.SetString("authorizationId", Request.Query.ElementAt(0).Value);
and now it works. Thank you all for the answers!
Upvotes: 0
Views: 759
Reputation: 800
You can store this data into session instead of class.as per below example
public class HomeController : Controller
{
public ActionResult ValidateTwitterAuth()
{
Session["verifierCode"] = Request.Query.ElementAt(2).Value;
//do some stuff
}
public String sendTweet(String NewTweet)
{
var userCreds = AuthFlow.CreateCredentialsFromVerifierCode(Session["verifierCode"], Container.authorizationId);
var user = Tweetinvi.User.GetAuthenticatedUser(userCreds);
user.PublishTweet(NewTweet);
return "sent!";
}
}
i have just change only verify code
Upvotes: 2