C# 如何访问 .ashx 文件中的会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11447196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access Session in .ashx file?
提问by Kashif R
I want to access some value(which is already set in.aspx file) in .ashx file. I tried to get that value using querystring, session etc but each time it failed. Can anyone suggest me how can we access session value in .ashx file?
我想访问 .ashx 文件中的一些值(已在 .aspx 文件中设置)。我尝试使用查询字符串、会话等获取该值,但每次都失败了。谁能建议我如何访问 .ashx 文件中的会话值?
采纳答案by mrd
In aspx file:
在 aspx 文件中:
Session.Add("filename", "Test.txt");
After you have set session value in aspx file. Use following to get the value in ashx file.
在 aspx 文件中设置会话值后。使用以下获取 ashx 文件中的值。
In ashx file:
在 ashx 文件中:
public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string Name = "";
if (context.Session["filename"] != null)
Name = context.Session["filename"].ToString();
}
}
回答by Chamika Sandamal
Try this,
尝试这个,
HttpContext.Current.Session
回答by Hans Ke?ing
In the ashx.cs file, also "implement" the interface System.Web.SessionState.IReadOnlySessionStateor System.Web.SessionState.IRequiresSessionState.
在 ashx.cs 文件中,还要“实现”接口System.Web.SessionState.IReadOnlySessionState或System.Web.SessionState.IRequiresSessionState.
You don't have to implement any method, just the presence of this makes the Session available (in readonly or read/write mode), through context.Session.
您不必实现任何方法,只要它的存在使 Session 可用(在只读或读/写模式下),通过context.Session.
The header would look like:
标题看起来像:
public class MyHandler: IHttpHandler, System.Web.SessionState.IReadOnlySessionState

