4

在我的 CS 文件中,我正在执行以下操作,它按预期工作。

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

但是,在我的 CSHTML 文件中,我正在执行以下操作,在这里,我收到一个关于Json无法在上下文中识别的错误。

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

如何解释/补救?谷歌搜索给了我 nada,零,ziltch ......

编辑

我已按照建议将程序集标签添加到我的 CONFIG 文件中,但我得到的错误是它对配置来说是未知的。这就是我的(根)CONFIG 的样子。

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

但是,我注意到我在 CONFIG 文件中确实有以下内容。我猜它是等价的。是吗?

<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>
4

2 回答 2

6

我能够重现您的问题并找到解决方案。至于为什么这个解决方案有效,我不能说,但这是我遵循的步骤。

1.从一个新的 MVC 4 项目(基本)中,我创建了一个带有以下标记的视图(注意 ReSharper 7 没有投诉)

@{
    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.编译并运行应用程序,收到以下错误: CS0103: The name 'Json' does not exist in the current context

3.尝试在View中导入命名空间,将程序集添加到Views web.config中,确保在项目中正确引用。这些都没有效果。

4.解决方案在编译部分的我的web.config中 添加了以下几行(<assemblies> <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies>)(主web.config,而不是Views文件夹之一)

  <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.重新编译,这是生成的 HTML 页面输出无错误

<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>
于 2013-11-05T21:40:32.827 回答
1

确保您System.Web.Helpers在项目中引用并将“复制本地”设置为true

于 2013-11-04T11:13:45.650 回答