How to access Session in .ashx file?

后端 未结 3 987
野趣味
野趣味 2020-12-08 10:08

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 su

相关标签:
3条回答
  • 2020-12-08 10:18

    In the ashx.cs file, also "implement" the interface System.Web.SessionState.IReadOnlySessionState or 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.

    The header would look like:

    public class MyHandler: IHttpHandler, System.Web.SessionState.IReadOnlySessionState
    
    0 讨论(0)
  • 2020-12-08 10:24

    In aspx file:

    Session.Add("filename", "Test.txt");
    


    After you have set session value in aspx file. Use following to get the value in ashx file.

    In ashx file:

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-08 10:42

    Try this,

    HttpContext.Current.Session
    
    0 讨论(0)
提交回复
热议问题