title: Json.net used in Web API but not when calling Json.Encode in Razor Template
body: <p>I am building a MVC 4 application with both traditional and Web API controllers. Since I'm also using EF5 with code-first I have navigation properties that lead to circular references. I am using <code>JsonIgnore</code> attributes on these navigation properties to avoid running into circular reference loops when serializing to Json.</p>
  
  <p>This approach works flawlessly for my Web API controllers, however, when using <code>Html.Raw(Json.Encode(Model))</code> in a traditional controller's Razor template, I get an exception due to the circular references: </p>
  
  <blockquote>
    <p>A circular reference was detected while serializing an object</p>
  </blockquote>
  
  <p>This exception occurs in <code>System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal</code>. Am I interpreting this correctly in that <code>Json.Encode</code> seems to use a different Json encoder (not json.net)? How can I use the configured Json.Net formatter used in Web API from inside Razor templates?</p>
  
  <p>I came up with a workaround where I use the following class</p>
  
  <pre><code>namespace MyMVCProject.Globals
  {
      public class Helper
      {
          public static string ToJson(object obj)
          {
              return JsonConvert.SerializeObject(obj, GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings);
          }
      }
  }
  </code></pre>
  
  <p>so I can use it inside the template: <code>@Html.Raw(MyMVCProject.Globals.Helper.ToJson(Model))</code></p>
  
  <p>This still feels awkward for something I'd expect to be default behaviour of <code>Json.Encode</code></p>