Json.Encode 在 CS 文件中工作,但在 CSHTML 中不起作用

2024-04-11

在我的 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,零,zilch......

Edit

我已经添加组件按照建议标记到我的配置文件,但我得到的错误是它对配置来说是未知的。这就是我的(根)配置的样子。

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

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

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.解决方案添加了以下行(<assemblies> <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies>)到编译部分中的 web.config(主要 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Json.Encode 在 CS 文件中工作,但在 CSHTML 中不起作用 的相关文章

随机推荐