Reputation: 369
I am trying to access some Session Variables in a Razor Pages application, but am met with NullReferenceException errors.
Currently, I set the variables in a method in the model associated with a "user login" page:
public void StoreUserData(string username, string name, int accesslevel)
{
HttpContext.Session.SetString("username", username);
HttpContext.Session.SetString("name", name);
HttpContext.Session.SetInt32("accesslevel", accesslevel);
}
I then call this method (and the method which gets the data to be stored) when the user submits the login form on the page (on post):
public IActionResult OnPost()
{
(string username, string name, int accesslevel) = Login();
StoreUserData(username, name, accesslevel);
ViewData["username"] = HttpContext.Session.GetString("username");
return Page();
}
I know that the session variables are being saved, and can be accessed (at least on the "login" page) because the "username" string is correctly displayed on the page using ViewData, as shown in the block above.
When I try to access the same session variables in another razor page, it doesn't work.
I want to check the session variables when the page loads. C# method (part of page model for second page):
public (string username, string name, int accesslevel) GetUserData()
{
string username = HttpContext.Session.GetString("username");
string name = HttpContext.Session.GetString("name");
int accesslevel = Convert.ToInt32(HttpContext.Session.GetInt32("accesslevel"));
return (username, name, accesslevel);
}
And C# block on html page to call this method (I don't know a better way to do this when the page loads):
@{
Test_1Model model = new Test_1Model();
(string username, string name, int accesslevel) = model.GetUserData();
<div>@username</div>
}
At the moment, I just want to display one of the session variables on this second page to check everything's working.
Finally, in startup.cs: Configure Services:
public void ConfigureServices(IServiceCollection services)
{
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(10);
});
services.AddHttpContextAccessor();
services.AddMemoryCache();
services.AddMvc();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
Configure:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
I know it's a lot of information, but I've tried to be as thorough as possible. Any help is greatly appreciated.
Upvotes: 2
Views: 1125
Reputation: 29996
This is caused by that Test_1Model model = new Test_1Model();
, you should use Model
instead of initializing a new Test_1Model
. For this new object, it is not related with your request, it's just an object.
Try.
@{
ViewData["Title"] = "Contact";
//ContactModel model = new ContactModel();
(string username, string name, int accesslevel) = Model.GetUserData();
<div>@username</div>
}
Upvotes: 1