title: ASP.NET MVC - JSON.NET extension method for controller
body: <p>I just added JSON.NET to my project, and I would like to create an extension method called JsonNet that behaves the same way as the Json method, but uses JSON.NET instead.</p>
  
  <p>I have a class here that extends JsonResult using JSON.NET:</p>
  
  <pre><code>public class JsonNetResult : JsonResult {
      public override void ExecuteResult(ControllerContext context) {
          if (context == null)
              throw new ArgumentNullException("context");
  
          var response = context.HttpContext.Response;
  
          response.ContentType = !String.IsNullOrEmpty(ContentType)
              ? ContentType
              : "application/json";
  
          if (ContentEncoding != null)
              response.ContentEncoding = ContentEncoding;
  
          var serializedObject = JsonConvert.SerializeObject(Data, Formatting.Indented);
          response.Write(serializedObject);
      }
  }
  </code></pre>
  
  <p>Underneath the ExecuteResult method, I tried to add these:</p>
  
  <pre><code>public static JsonNetResult JsonNet(this Controller controller, object data) {
      var result = new JsonNetResult();
      result.Data = data;
      return result;
  }
  
  public static JsonNetResult JsonNet(this Controller controller, object data, JsonRequestBehavior behavior) {
      var result = new JsonNetResult();
      result.Data = data;
      result.JsonRequestBehavior = behavior;
      return result;
  }
  </code></pre>
  
  <p>And then I have my controller:</p>
  
  <pre><code>public class SomethingController : Controller {
      public ActionResult SomeAction() {
          object data = SomeClass.GetData();
          return JsonNet(data, JsonRequestBehavior.AllowGet);
      }
  }
  </code></pre>
  
  <p>And the compiler cannot find the JsonNet method. Even when I try this:</p>
  
  <pre><code>return ((Controller)this).JsonNet(data, JsonRequestBehavior.AllowGet);
  </code></pre>
  
  <p>it still doesn't work.</p>
  
  <p>If I copy the code in JsonNet into SomeAction, it works just fine, so I know SomethingController can see JsonNetResult.</p>
  
  <p>If it helps, the two classes are in separate namespaces.</p>