简体   繁体   English

我如何从会话字节的数组中提取? 在会话内部也写入了字节的数组

[英]How can I take from Session byte's array? Inside the session was written byte's array too

Is it byte inside the session or data will be converted into the string after writing? 是会话中的字节还是写入后将数据转换为字符串? if yes I think I can take it like this: 如果是的话,我想我可以这样:

var res = Encoding.UTF8.GetBytes(Session["session_state"]);

or can I take it "as is" without converting into the byte array? 还是可以按原样使用它而不转换为字节数组? like: 喜欢:

var res = Session["session_state"] as bytes[]; // or smth. like that

The data isn't converted. 数据未转换。 If the session object is serialized (depending on how it is stored), then it is deserialized before you get access to it again. 如果会话对象已序列化(取决于它的存储方式),则在您再次访问它之前,将对它进行反序列化。

Just cast the value to a byte array: 只需将值转换为字节数组即可:

var res = Session["session_state"] as byte[];

or: 要么:

var res = (byte[])Session["session_state"];

Side note: A byte array can't reliably be converted to a string using UTF-8 encoding. 旁注:无法使用UTF-8编码将字节数组可靠地转换为字符串。 UTF-8 is used the other way around, ie converting a string to bytes and then back. 使用UTF-8的另一种方法,即将字符串转换为字节,然后转换为字节。 To make a string from bytes you would rather use something like base64. 要从字节创建字符串,您宁愿使用诸如base64之类的东西。

You will always get what you stored in the session regardless of the mode you are using for the session state (inproc, state server, ...) 无论会话状态使用哪种模式(过程,状态服务器等),您都将始终获得会话中存储的内容

So the answer will be 所以答案将是

var res = Session["session_state"] as byte[];

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

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