简体   繁体   English

会话状态下的C#类?

[英]c# class in a session state?

My senior project is building a reservation system in ASP.NET/C#. 我的高级项目是使用ASP.NET/C#构建预订系统。 Part of my senior project is to have c# classes (and basically use everything ive learned in the past few years). 我的高级项目的一部分是拥有c#类(并基本上使用过去几年中学到的所有ive)。 One thing Im trying to do is after I instantiate a new "user" class I need it to travel between the pages. 我试图做的一件事是在实例化一个新的“用户”类之后,我需要它在页面之间移动。 I know session states holds variables, so I figured a session state would work where I can simply type "Session["blah"]." 我知道会话状态包含变量,因此我认为可以在只需键入“ Session [“ blah”]的会话状态下工作。” and have access to its members. 并有权访问其成员。 But I dont see that happening. 但我看不到这种情况。 I realize session states are HTTP context, so i doubted it would work anyway. 我意识到会话状态是HTTP上下文,因此我怀疑它是否仍然有效。 But is there any other way in which I can accomplish what I need without instantiating a new user class every time? 但是,是否还有其他方法可以完成我所需的而无需每次都实例化一个新的用户类? I know its a webpage...but im also trying to make it as much as a functional online program as I can. 我知道它的网页...但是我也想尽我所能使它成为一个功能强大的在线程序。

Just for coder's sake, heres the code snippet im working with: 仅出于编码人员的考虑,以下代码段可用于:

    cDatabaseManager cDM = new cDatabaseManager();

    string forDBPass = Encryptdata(pass_txt.Text.ToString());
    string fullName = fname_txt.Text.ToString() + " " + lname_txt.Text.ToString();

    cDM.regStudent(email_txt.Text.ToString(), forDBPass, fullName, num_txt.Text.ToString(), carrier_ddl.SelectedValue.ToString(), this);

    //ADD - getting a cStudent
    cUser studentUser = new cStudent(fullName, forDBPass, email_txt.Text.ToString());

    //ADD - session states
    Session["cStudent"] = studentUser;

    //Session["cStudent"].      //session state will not work with what I am doing
    //ADD - transfer to campus diagram

Thanks in advance!! 提前致谢!!

EDIT: 编辑:

I want to thank all of you who posted and commented! 我要感谢所有发表评论的人! Ive learned alot from this short discussion. 我从这次简短的讨论中学到了很多东西。 All your answers helped me understand! 您所有的答案都帮助我理解了!

Session stores item as objects. 会话将项目存储为对象。 As long as your class inherits from Object (which it does) you can store it there. 只要您的类继承自Object(确实如此),您就可以将其存储在那里。 Quick caveat, it stores that object using Serialization, so your class must be serializable. 快速警告,它使用序列化存储该对象,因此您的类必须可序列化。

Add a property to your class like so: 像这样向您的班级添加一个属性:

public cStudent CurrentStudent
{
    get {
        if(Session["CurrentUser"] == null)
            return null;

        return (cStudent)Session["CurrentUser"];
    }
    set {
        Session["CurrentUser"] = value;
    }
}

From your comment: 根据您的评论:

The issue is when I type "Session["cStudent"]." 问题是当我键入"Session["cStudent"]." I don't have access to my functions. 我无权使用我的功能。 Example: Session["cStudent"].getName() does not give my functionality. 示例: Session["cStudent"].getName()没有提供我的功能。

This is because the [] indexer for Session sets/returns object s. 这是因为Session[]索引器设置/返回object s。 The compiler does not know that you stored a cUser object and so you can't access the properties directly without a cast: 编译器不知道您存储了cUser对象,因此如果没有cUser ,就无法直接访问属性:

string name = ((cUser)Session["cStudent"]).getName();

There are two things that could go wrong here: 这里有两件事可能出错:

  1. If Session["cStudent"] is null you will get a NullReferenceException 如果Session["cStudent"]null ,则将获得NullReferenceException
  2. If Session["cStudent"] is not really a cUser you will get an InvalidCastException 如果Session["cStudent"]不是真正的cUser您将收到InvalidCastException

You should check these conditions and react appropriately if one of them is true. 您应检查这些条件,如果其中之一为真,则应做出适当的反应。

Also, as others have pointed out, the cUser class needs to be marked as Serializable in order to be stored in Session state. 同样,正如其他人指出的那样, cUser类需要标记为Serializable才能存储在Session状态中。

When retrieving an object value from session state cast it to appropriate type. 从会话状态检索对象值时,将其强制转换为适当的类型。

[Serializable]    
public class student
    {
      public string FirstName { get; set; }
      public string LastName { get; set; }
    }

In Page1: 在第1页中:

student s1 = new student();
s1.FirstName ="James";
s1.LastName = "Bond";
Session["blah"] = s1;

And when you want to access Session["blah"] in page 2 当您要访问第2页中的Session [“ blah”]时

student s2 = (Session["blah"] !=null ? (student)Session["blah"] : null);

Now you can access properties of s2 as s2.FirstName, s2.LastName 现在,您可以将s2的属性作为s2.FirstName,s2.LastName访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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