Developer forum

Forum » CMS - Standard features » HTTP handler in DW context

HTTP handler in DW context

Lars Larsen
Reply

Hi,

I have made a HTTP handler that looks up some information in DW and returns this information to the browser. The handler should be used in an AJAX call. But how do I make my HTTP handler run in DW context? Right now I have my HTTP handler running, but it can not get the current pageview which I need access to, because the HTTP handler runs before DW.


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Lars,

 

An HTTP handler fired from an AJX call runs independent of Dynamicweb; it's a separate call with its own HTTP Context. Therefore, there is no Pageview from Dynamicweb.

 

What are you trying to accomplish? There may be other ways to do what you're trying to do.

 

Cheers,

 

Imar

 
Lars Larsen
Reply

Hi Imaar,

I need to get a special price from an eCommerce productlist. When the user enters a quantity for a product, a price is looked up (by an AJAX call) in a custom table in the database, and it should only be possible to look up the price if the user is logged in. Therefore I need the DW context. I thought that making the AJAX call by calling a HTTP handler would be a more clean solution instead of calling a DW page with a DW custommodule on, that would return the special price. Actually I have made this solution now, because I couldn't get the HTTP handler solution to work. But when using the custommodule solution I have to deal with output on the returned page that I don't need (e.g empty lines in the top of the page and a javascript inserted automatically by DW at the bottom of the page). As just mentioned I couldn't make a layout (Designs & Layout) that didn't output anything else than my price! Any idea why?

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Lars

 

In your HTTP handler you can see if a user is logged in by doing this:

Dim u As New Frontend.Extranet
If u.LoggedIn Then
	'User is logged in
End If

 

But if you use the custom module approach you are doing now, you can just interrupt the response and change what you are returning from the view - even if it is inside a pageview. I.e. if you want a json response, do something like this inside your GetContent method:

 

Response.Clear()
Response.AddHeader("Content-type", "text/json")
Response.AddHeader("Content-type", "application/json")
Response.ContentType = "application/json"
Response.Write(yourJsonString)
Response.End()
Votes for this answer: 1
 
Lars Larsen
Reply

Hi Nicolai,

thanks for your response. Of course your'e right, manipulating the response will do the trick. But I used Frontend.Extranet (as you suggest) from my HTTP handler and now it works :-)

 
Mikkel Høst
Reply

If anyone else is wondering how to do this with a handler you just need to inherit from IRequriesSessionState

 

public class SessionHeartbeat : IHttpHandler, IRequiresSessionState 
    {
        HttpContext htc = HttpContext.Current;
        int hoursToKeepAlive = 9;        
        public void ProcessRequest(HttpContext context)
        {
            if (User.GetCurrentUser() != null)
            {
                string sessionKey = "sessionStart|" + User.GetCurrentUser().UserName;
                DateTime? sessionStart = context.Session[sessionKey] as DateTime?;
                if (sessionStart == null)
                {
                    context.Session[sessionKey] = DateTime.Now;

                }
                sessionStart = context.Session[sessionKey] as DateTime?;

                if (DateTime.Now >= sessionStart.Value.AddHours(hoursToKeepAlive))
                {
                    stop(context);
                }
                else
                {//Keep the session alive                    
                    context.Session["KeepAlive"] = DateTime.Now;
                    context.Response.Write("running");
                }
            }
            else
            {
                //no user "safe stop"
                stop(context);
            }
           
        }
        private void stop(HttpContext context)
        {
            context.Response.Write("stop");
            context.Response.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

 

 

 

 
Sten Hougaard
Reply

@Mikkel: Your code example would that be executed for every request to the Dynamicweb Solution?

 
Mikkel Høst
Reply

Hi Sten.

Nope, only when you request the handler 

 

You must be logged in to post in the forum