4

In my CS file I'm executing the following and it works as expected.

using System.Web.Helpers;
String json = System.Web.Helpers.Json.Encode(null);

However, in my CSHTML file I'm executing the following, and here, I get an error about Json not being recognized in the context.

@{ Layout = null; }
@using TestService.ServiceReference;
@using System.Web.Helpers;
<!DOCTYPE html>
<html>
...
<script type="text/javascript">
  var output3 = "! @Html.Raw(Json.Encode(ViewBag.MyArray))";
...

How can that be explained/remedied? Googling gave me nada, zero, ziltch...

Edit

I've added assemblies tag to my CONFIG file as suggested but the error I'm getting is that it's an unknown to the configuration. This is what my (root) CONFIG looks like.

<system.web>
  <compilation debug="true" targetFramework="4.0" />
  <assemblies>
    <add assembly="System.Web.Helpers, Version=2.0.0.0, 
                  Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  </assemblies>
  ...

However, I've noticed that I do have the following in the CONFIG file instead. I'm guessing it's equivalent. Is it?

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

2 Answers2

6

I was able to recreate your problem and also found a solution. As to why this solution works, I cannot say, but here are the steps I followed.

1.From a fresh MVC 4 project (Basic), I created a View with the following mark up (note ReSharper 7 had no complaints)

@{
    ViewBag.Title = "Index";
    ViewBag.Array = new string[] {"apples", "oranges", "bananas"};
}

<script type="text/javascript">
    var stuff = "! @Html.Raw(Json.Encode(ViewBag.Array))";
    alert(stuff);
</script>

<h2>Index</h2>

2.Compiled and ran the application, received the following error: CS0103: The name 'Json' does not exist in the current context.

3.Attempted to import namespace in View, added assembly to Views web.config, ensured proper reference in project. None of these had an effect.

4.The Solution Added the following lines (<assemblies> <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies>) to my web.config in the compilation section (the primary web.config, not the Views folder one)

  <system.web>
    <httpRuntime targetFramework="4.5" />
    <compilation debug="true" targetFramework="4.5">
      <!--Add the following.  Ensure compilation is not self closing,
          it was on mine-->    
      <assemblies>
        <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>

5.Recompile and here is the resulting HTML page output sans error

<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="/Content/site.css" rel="stylesheet"/>

    <script src="/Scripts/modernizr-2.6.2.js"></script>

</head>
<body>


<script type="text/javascript">
    var stuff = "! ["apples","oranges","bananas"]";
    alert(stuff);
</script>

<h2>Index</h2>


    <script src="/Scripts/jquery-1.8.2.js"></script>


</body>
</html>
Tommy
  • 39,592
  • 10
  • 90
  • 121
  • #4 is usually the reason. You let the IDE resolve it for you. I suspect @KonradViltersten did not – Dave Alperovich Nov 06 '13 at 01:20
  • @KonradViltersten - the mapping rules that you found in your web.config were present in mine as well and I still got the error on the clean project. Also, if you look closely at the config I posted, the are a sub element of the compilation XML element which is why I made the comment about the self-closing compilation tag, you will need to open that up : – Tommy Nov 06 '13 at 15:05
  • 1
    Awesome digging, bro. Error of high weirdness level... :) – Konrad Viltersten Nov 11 '13 at 08:13
1

Make sure you're referencing System.Web.Helpers in your project and set "copy local" to true

Florian Haider
  • 1,892
  • 18
  • 23
  • I'm executing the code in CS file successfully **and** I have using in both CS and CSHTML file. Is that equivalent to *making sure to reference System.Web.Helpers* or is there anything more I can do? Also, please elaborate on *copy local = true*. – Konrad Viltersten Nov 06 '13 at 08:32
  • Look at BjarkeCKs answer to this question http://stackoverflow.com/questions/12682128/mvc-4-assembly-reference-missing-for-json-encode – Florian Haider Nov 07 '13 at 20:48
  • Why? It doesn't address my issue at all. I'm not missing the assemblies. They're just not found on the client-side. – Konrad Viltersten Nov 11 '13 at 08:14