Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json.Encode working in CS file but not in CSHTML

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>
like image 755
Konrad Viltersten Avatar asked Mar 22 '23 19:03

Konrad Viltersten


1 Answers

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>
like image 101
Tommy Avatar answered Mar 24 '23 09:03

Tommy