(ASP.NET Core Razor Pages) Unable to access Session Variables from other pages
up vote
2
down vote
favorite
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.
asp.net-core session-variables nullreferenceexception razor-pages session-storage
add a comment |
up vote
2
down vote
favorite
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.
asp.net-core session-variables nullreferenceexception razor-pages session-storage
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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.
asp.net-core session-variables nullreferenceexception razor-pages session-storage
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.
asp.net-core session-variables nullreferenceexception razor-pages session-storage
asp.net-core session-variables nullreferenceexception razor-pages session-storage
asked Nov 22 at 16:07
bbbb
373
373
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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>
}
It works! Thank you very much
– bbbb
Nov 23 at 8:24
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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>
}
It works! Thank you very much
– bbbb
Nov 23 at 8:24
add a comment |
up vote
1
down vote
accepted
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>
}
It works! Thank you very much
– bbbb
Nov 23 at 8:24
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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>
}
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>
}
answered Nov 23 at 7:12
Tao Zhou
4,34021028
4,34021028
It works! Thank you very much
– bbbb
Nov 23 at 8:24
add a comment |
It works! Thank you very much
– bbbb
Nov 23 at 8:24
It works! Thank you very much
– bbbb
Nov 23 at 8:24
It works! Thank you very much
– bbbb
Nov 23 at 8:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434734%2fasp-net-core-razor-pages-unable-to-access-session-variables-from-other-pages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown