简体   繁体   中英

How to type cast of a session object into a class object in c#

i have a class called user. I put that class object into session. My question is, how can i retrieve that object into another page?

var user = obj.user;
var username = obj.username;
var name = obj.cn;
var title = obj.title;
var department = obj.department;
User obj= new User();
Session["User"] = obj;

Another Page:

Index.cshtml

<div>
<h4> Hi @  i need the username here </h4>
<h4> i need department here</h4>
</div>

help me to access the session object.

In your view,use

  @using Microsoft.AspNetCore.Http;
  <h4>@Context.Session.GetString("User")</h4>

or you can pass Session["User"] into a TempData variable and catch that TempData variable into your index method.so that you can access it into your view.

I am sure you have found the solution but if not you can simple do this step at the top of the view

`@{ User u = (User)Session["User"];}`

then on page you can simple call any where like

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM