Getting a session in the ashx Http handler

I am using the ashx Http Handler file to display images. I used the Session object to get the image and return in response

Now the problem is that I need to use my own session object, but this is nothing like Wrapper on HttpSession State. But when I try to get an existing user session object, it creates a new one ... it does not show session data, I checked the session identifier, which is also different How can I get an existing session in an ashx file?

Note. When I use ASP.NET Sesssion, it works fine

[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class GetImage : IHttpHandler, System.Web.SessionState.IRequiresSessionState { 
+4
source share
3 answers

If you want to access your session state from ASHX or HttpHandler, you need to implement IReadOnlySessionState or IRequiresSessionState if you need read / write access.

+2
source

The fact that it should be irrelevant is provided that the request is generated from the request from the exsiting session; I suppose it should be - but he can pay to check exactly how the request is formed. Always pay to get back to basics :)

Assuming this is normal, here is how I did it:

 string sessionId = string.Empty; System.Web.SessionState.SessionIDManager sessionIDManager = new System.Web.SessionState.SessionIDManager(); bool supportSessionIDReissue; sessionIDManager.InitializeRequest(httpContext, false, out supportSessionIDReissue); sessionId = sessionIDManager.GetSessionID(httpContext); if (sessionId == null) { // Create / issue new session id: sessionId = sessionIDManager.CreateSessionID(httpContext); } 

At the end of this parameter, the sessionId variable will contain (should) be an existing session identifier or a newly created one that can be reused later.

+1
source

you can just use Actionresult and not a handler for this

 return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg"); 

or

return (new FileResult (Pathtoimage, "image / jpeg"));

which should make things easier for you since you will use controll / action as your URL

t

 <img src="/Images/showImage/1"> 

you can have your actions with something like pulling from db as bytes streaming, validation check, etc.

0
source

Source: https://habr.com/ru/post/1302275/


All Articles