2

I created custom ActionResult (simplified):

public class FastJSONResult : ActionResult
{
    public string JsonData { get; private set; }

    public FastJSONResult(object data)
    {
        JsonData = JSON.Instance.ToJSON(data);
    }

    public override void ExecuteResult(ControllerContext context)
    {
        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = "application/json";
        response.Output.Write(JsonData);
    }
}

And use it from my WebApi controller:

public ActionResult GetReport()
{
   var report = new Report();
   return new FastJSONResult(report);
}

Now the problem is, despite the fact that in FastJSONResult constructor my object serializes perfectly, ExecuteResult never gets called and in response I end up with object like

{"JsonData":"{my json object as a string value}"}

What am I doing wrong?

6
  • 1
    Your response looks good, that is the same response you would get if you used return Json(report, JsonRequestBehavior.AllowGet);. Have you tried that? Commented Oct 10, 2013 at 19:45
  • @DZL ok, maybe, but that's not nice, isn't it? Commented Oct 10, 2013 at 20:32
  • @DZL also, my point is, why 'ExecuteResult' is not being called? Commented Oct 10, 2013 at 20:34
  • Do you override OnResultExecuting or something like that on your controller? Commented Oct 10, 2013 at 20:35
  • @DZL no, but i think this has something to do with WebApi andRegular controllers difference in pipeline. Seems like in WebApi i have to use formatters, not ActionResults Commented Oct 10, 2013 at 20:38

1 Answer 1

1

Solved it with custom formatter (simplified to post less code)

public class FastJsonFormatter : MediaTypeFormatter
{
  private static JSONParameters _parameters = new JSONParameters()
  {
    public FastJsonFormatter()
    {
      SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
      SupportedEncodings.Add(new UTF8Encoding(false, true));
    }

    public override bool CanReadType(Type type)
    {
      return true;
    }

    public override bool CanWriteType(Type type)
    {
      return true;
    }

    public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
    {
        var task = Task<object>.Factory.StartNew(() => JSON.Instance.ToObject(new StreamReader(readStream).ReadToEnd(), type));
        return task;
    }

    public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
    {
      var task = Task.Factory.StartNew(() =>
      {
         var json = JSON.Instance.ToJSON(value, _parameters);
         using (var w = new StreamWriter(writeStream)) w.Write(json);  
      });
      return task;
    }
}

In WebApiConfig.Register method:

config.Formatters.Remove(config.Formatters.JsonFormatter);
config.Formatters.Add(new FastJsonFormatter());

And now I receive json object properly: Sample

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.