Custom IHttpHandler

Alfred Brockotter asked on February 20, 2014 10:24

Hello Guys,

I'm new to kentico (just working with kentico 7 for 3 weeks now) and i have a question about implementing a custom HttpHandler.

What i have done so far: I've created a new class library and created a httphandler:

public class SpelenHandler : IHttpHandler
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            // prepare the user's ID
            try
            {
                int gameId = 0;

                int.TryParse(context.Request.QueryString["gameid"].ToString(), out gameId);

                // get the object from database according to the data class name and ID
                IDataClass gameObj = DataClassFactory.NewDataClass("Custom.Game", gameId);
                // acquire the user's name
                int TimesPlayed = Convert.ToInt32(gameObj.GetValue("TimesPlayed").ToString());
                TimesPlayed = TimesPlayed + 1;
                // change the user's full name
                gameObj.SetValue("TimesPlayed", TimesPlayed);
                // update the database record
                gameObj.Update();

                context.Response.Write(JsonConvert.SerializeObject(true));

            }
            catch {
                context.Response.Write(JsonConvert.SerializeObject(false));
            }
        }

        #endregion
    }

I registered that handler in IIS and adjusted the web.config of my kentico instance. Now i am able to send jquery ajax requests to my handler and update my documenttype. This works perfectly! But a big disadvantage is compiling it against different versions of kentico (because of the reference to CMS.DataEngine.dll etc).

So therefor:

  1. Is it possible to define this httphandler inside the app_code of kentico?

  2. And what other options do i have for sending/recieving json ajax requests to/from the "backend"? I played already a bit with the service.asmx but i don't like that to much because it depends on the AjaxControlToolkit.

Kind regards, Alfred

Recent Answers


Juraj Ondrus answered on February 21, 2014 02:25

Hi,

  1. Yes, you can add your code to App_Code as usual
  2. You can use e.g. PageMethod or any other services. There is nothing special about it. Or, you can also use the REST services.

Best regards,
Juraj Ondrus

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.