Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can i get session in generic handler(.ashx page)

please answer me
Posted
Comments
shakil0304003 4-Feb-11 1:51am    
Use google 1st!!! http://www.google.com/search?hl=en&client=opera&hs=6Wv&rls=en&channel=suggest&q=session+in+generic+handler+in+c%23&aq=f&aqi=&aql=&oq=

Hey you can set/get Session as below, n if SessionState object not exist you first need to add it. Dis code not required any other configuration. This demo i used to store CAPTHCA in session.
C#
using System;
using System.Web;
using System.Drawing;
using System.Web.SessionState;

public class captcha : IHttpHandler, IRequiresSessionState
{

   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/jpeg";
        CatpchaImage captcha = new CatpchaImage();
        string str = captcha.DrawNumbers(5);
        if (context.Session[ CatpchaImage.SESSION_CAPTCHA] == null) context.Session.Add(CatpchaImage.SESSION_CAPTCHA, str);
        else
        {
            context.Session[ CatpchaImage.SESSION_CAPTCHA] = str;
        }
        Bitmap bmp = captcha.Result;
        bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
 
    
    
    public bool IsReusable {
        get {
            return true;
        }
    }

}
 
Share this answer
 
Comments
krjaan 1-May-12 8:28am    
Answer is quite helpful...
but now i have another problem
I need to call mysomehandler.ashx with the post back of my content page
the problem is i have a form with post in my master page
and mysomehandler.ashx can only be called with post of my content page's form tag
pleas help
but in the generic handler 'HttpContext.Current.Session' is always null
why this?
 
Share this answer
 
Yes, you will get the session in handler.
HttpContext.Current.Session
 
Share this answer
 
v2
Comments
OriginalGriff 4-Feb-11 2:39am    
The OP wrote:
"can you say how i didnt getting now"
Check your web.config, whether .ashx reference is added under <httphandlers>...
ie
<add verb="*" path="*.ashx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

Then try using
context.session["sample"] = "";


It will allow to add/handle session.

if not add sessionstate namespace along with the above mentioned code.
 
Share this answer
 
Handlers are the lightest objects to operate Request/Response, so it will only reference/load as much data as you describe for it ...

For this specific case, all you need to do is implement the IRequireSessionState in the handler.

As the example right here:
public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState 


Cheers.
 
Share this answer
 
Use IReadOnlySessionState if you only need to read values from Session.
Use IRequireSessionState if you need to store values in Session or full access to session.

Example: IReadOnlySessionState
C#
using System;
using System.Web;
using System.Web.SessionState;

public class DownloadHandler : IHttpHandler, IReadOnlySessionState
{
   public bool IsReusable { get { return true; } }

   public void ProcessRequest(HttpContext ctx)
   {
       ctx.Response.Write(ctx.Session["fred"]);
   }
}
 
Share this answer
 
Hi Sabitha,

You can use session in .ashx page. Please check following example:

You can use:
C#
string UserName = Convert.ToString(HttpContext.Current.Session["UserName"]);


Full Example:
C#
public class Handler : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
        string UserName = Convert.ToString(HttpContext.Current.Session["UserName"]);
    }
    public bool IsReusable {
        get {
            return false;
        }
    }
}


Thanks,
Imdadhusen
 
Share this answer
 
yes....


C#
public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {

       
        string User = context.Session["Uname"].ToString();
        context.Response.Write(User );
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900