public void ProcessRequest(HttpContext context)
        {
            MediaResponse response = null;

            var action = context.Request["action"];

            if (IsValidRequest(context) && !string.IsNullOrEmpty(action))
            {
                switch (action.ToLower())
                {
                    case "config":
                        response = ProcessConfigRequest(context);
                        break;
                    case "folderlist":
                        response = ProcessFolderListRequest(context);
                        break;
                    case "upload":
                        response = ProcessUploadRequest(context);
                        break;
                }
            }

            // Set success flag
            if (response != null)
                response.success = true;
            else
                response = new MediaResponse { success = false };

            context.Response.Clear();
            context.Response.Charset = "UTF-8";
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.Cache.SetAllowResponseInBrowserHistory(true);

            var format = context.Request["format"];
            switch (format)
            {
                case "json":
                    // Format as JSON 
                    // Weirdly, this should be set to text/html to make it respond as json according to:
                    // http://stackoverflow.com/questions/6934393/resource-interpreted-as-document-but-transferred-with-mime-type-application-jso
                    context.Response.ContentType = @"text/html";

                    context.Response.Write(new JavaScriptSerializer().Serialize(response));

                    break;
                default:
                    // Format as XML
                    context.Response.ContentType = @"text/xml";

                    var serializer = new XmlSerializer(response.GetType());
                    serializer.Serialize(context.Response.OutputStream, response);

                    break;
            }
            

            context.Response.End();
        }
        public void ProcessRequest(HttpContext context)
        {
            MediaResponse response = null;

            var action = context.Request["action"];

            if (IsValidRequest(context) && !string.IsNullOrEmpty(action))
            {
                switch (action.ToLower())
                {
                case "config":
                    response = ProcessConfigRequest(context);
                    break;

                case "folderlist":
                    response = ProcessFolderListRequest(context);
                    break;

                case "upload":
                    response = ProcessUploadRequest(context);
                    break;
                }
            }

            // Set success flag
            if (response != null)
            {
                response.success = true;
            }
            else
            {
                response = new MediaResponse {
                    success = false
                }
            };

            context.Response.Clear();
            context.Response.Charset = "UTF-8";
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.Cache.SetAllowResponseInBrowserHistory(true);

            var format = context.Request["format"];

            switch (format)
            {
            case "json":
                // Format as JSON
                // Weirdly, this should be set to text/html to make it respond as json according to:
                // http://stackoverflow.com/questions/6934393/resource-interpreted-as-document-but-transferred-with-mime-type-application-jso
                context.Response.ContentType = @"text/html";

                context.Response.Write(new JavaScriptSerializer().Serialize(response));

                break;

            default:
                // Format as XML
                context.Response.ContentType = @"text/xml";

                var serializer = new XmlSerializer(response.GetType());
                serializer.Serialize(context.Response.OutputStream, response);

                break;
            }


            context.Response.End();
        }