我想在.ashx文件中访问一些值(这些值已经在.aspx文件中设置)。我尝试使用查询字符串、会话等获取该值,但每次都失败了。有人能建议我如何在.ashx文件中访问会话值吗?
我想在.ashx文件中访问一些值(这些值已经在.aspx文件中设置)。我尝试使用查询字符串、会话等获取该值,但每次都失败了。有人能建议我如何在.ashx文件中访问会话值吗?
System.Web.SessionState.IReadOnlySessionState
或者 System.Web.SessionState.IRequiresSessionState
。您不必实现任何方法,只需存在此内容,即可通过context.Session
访问 Session(以只读或读写模式)。
头部应如下所示:
public class MyHandler: IHttpHandler, System.Web.SessionState.IReadOnlySessionState
public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
- Scott R. FrostSession.Add("filename", "Test.txt");
在aspx文件中设置了session值之后,使用以下代码在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