0

I'm using a custom JsonpResult class that I found in my searching here on SO. I've read through the code and I understand how it works (at least I think I do), BUT... for some reason when I serialize my Object, I'm getting duplicate strings... Why would this be?

Here's the custom class

/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. All rights reserved.
 *
 * Content of this class was mostly derived from the original
 * JsonResult class in the System.Web.Mvc 2.0 RTM Assembly. This
 * has beeen slightly extended for use with JSONP calls.
 *
 * This software is subject to the Microsoft Public License (Ms-PL).
 * A copy of the license can be found in the license.htm file included
 * in this distribution.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * ***************************************************************************/
namespace System.Web.Mvc
{
    using System;
    using System.Text;
    using System.Web;
    using System.Web.Mvc.Resources;
    using System.Web.Script.Serialization;

    public class JsonpResult : ActionResult
    {

        public JsonpResult() { }

        public Encoding ContentEncoding { get; set; }

        public string ContentType { get; set; }

        public object Data { get; set; }

        public string JsonCallback { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            this.JsonCallback = context.HttpContext.Request["jsoncallback"];

            if (string.IsNullOrEmpty(this.JsonCallback))
                this.JsonCallback = context.HttpContext.Request["callback"];

            if (string.IsNullOrEmpty(this.JsonCallback))
                throw new ArgumentNullException("JsonCallback required for JSONP response.");

            HttpResponseBase response = context.HttpContext.Response;

            if (!String.IsNullOrEmpty(ContentType))
            {
                response.ContentType = ContentType;
            }
            else
            {
                response.ContentType = "application/javascript";
            }
            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }
            if (Data != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                response.Write(string.Format("{0}({1});", this.JsonCallback, serializer.Serialize(Data)));
            }
        }
    }

    //extension methods for the controller to allow jsonp.
    public static class ContollerExtensions
    {
        public static JsonpResult Jsonp(this Controller controller, object data)
        {
            JsonpResult result = new JsonpResult();
            result.Data = data;
            result.ExecuteResult(controller.ControllerContext);
            return result;
        }
    }
}

Here's my Controller Action

public class VimeoController : Controller
{

    //
    // GET: /Vimeo/
    public JsonpResult Read()
    {

    Models.ViewModels.JsonViewModel JsonResponse;

        try
        {
            VimeoSharp.APIs.VimeoSimple VimeoSimple = new VimeoSharp.APIs.VimeoSimple();
            // Tell VimeoSharp what channel to pull it's videos from (193328)
            List<VimeoSharp.Video> VimeoList = VimeoSimple.ChannelVideos("193328");

            // Create a viewmodel list of videos to be used.
            // we're only using id and title for this bit.
            List<Models.Pocos.Vimeo> videoList = new List<Models.Pocos.Vimeo>();
            foreach (VimeoSharp.Video record in VimeoList)
            {
                videoList.Add(new Models.Pocos.Vimeo
                {
                    id = 1, //Int32.Parse(record.ID),
                    title = "a" //(record.Title.Length > 26 ? record.Title.Substring(0, 25) + "..." : record.Title)
                });
            };

            JsonResponse = new Models.ViewModels.JsonViewModel
            {
                results = videoList,
                success = true
            };
        }
        catch {
            JsonResponse = new Models.ViewModels.JsonViewModel
            {
                results = null,
                success = false
            };
        }

        // a failed response
        return this.Jsonp(JsonResponse);
    }

}

And here's the output result.

CALLBACK1001({"results":[{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"}],"success":true});CALLBACK1001({"results":[{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"},{"id":1,"title":"a"}],"success":true});

Why on earth is my JsonResponse being serialized twice?

enter image description here

Community
  • 1
  • 1
Chase Florell
  • 46,378
  • 57
  • 186
  • 376

1 Answers1

2

The problem is in your extension method:

public static class ContollerExtensions
{
    public static JsonpResult Jsonp(this Controller controller, object data)
    {
        JsonpResult result = new JsonpResult();
        result.Data = data;
        //result.ExecuteResult(controller.ControllerContext); <-- Remove this !!!
        return result;
    }
}

Notice the line I commented. You are basically invoking the result twice. It's the ASP.NET MVC framework's responsibility to invoke the ExecuteResult method on the ActionResult, not yours.

Also if you found this code on StackOverflow why is it Copyright (c) Microsoft Corporation. All rights reserved. :-)

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928