[英]How to access Session in .ashx file?
我想在 .ashx 文件中访问一些值(已经在 .aspx 文件中设置)。 我尝试使用查询字符串、会话等获取该值,但每次都失败了。 谁能建议我如何访问 .ashx 文件中的会话值?
在 ashx.cs 文件中,还“实现”接口System.Web.SessionState.IReadOnlySessionState
或System.Web.SessionState.IRequiresSessionState
。
您不必实现任何方法,仅此方法的存在就可以通过context.Session
使 Session 可用(以只读或读/写模式)。
标题看起来像:
public class MyHandler: IHttpHandler, System.Web.SessionState.IReadOnlySessionState
在 aspx 文件中:
Session.Add("filename", "Test.txt");
在 aspx 文件中设置会话值后。 使用以下获取 ashx 文件中的值。
在 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();
}
}
试试这个,
HttpContext.Current.Session
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.