在CS文件,但不是在CSHTML Json.En code工作 [英] Json.Encode working in CS file but not in CSHTML

查看:221
本文介绍了在CS文件,但不是在CSHTML Json.En code工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的CS文件,我执行以下和它的作品如预期。

 使用System.Web.Helpers;
JSON字符串= System.Web.Helpers.Json.En code(NULL);

不过,在我CSHTML文件我执行以下命令,在这里,我得到一个错误的的Json 的不被认可的背景下。

  @ {布局= NULL; }
@using TestService.ServiceReference;
@using System.Web.Helpers;
<!DOCTYPE HTML>
< HTML和GT;
...
<脚本类型=文/ JavaScript的>
  VAR OUTPUT3 =@ Html.Raw(Json.En code(ViewBag.MyArray))!;
...

那怎么可以解释/补救?谷歌搜索给我虚无缥缈,零ziltch ...

修改

我添加的组件的标签我的CONFIG文件的建议,但我得到的错误是,它是一个默默无闻的配置。这就是我的(根)CONFIG的样子。

 <&的System.Web GT;
  <编译调试=真targetFramework =4.0/>
  <&集会GT;
    <添加组件=System.Web.Helpers,版本= 2.0.0.0,
                  文化=中性公钥= 31BF3856AD364E35/>
  < /组件>
  ...

不过,我已经注意到,我必须在CONFIG文件下面来代替。我猜这是等价的。是吗?

 <&运行GT;
  < assemblyBinding的xmlns =瓮:架构 - 微软COM:asm.v1>
    < dependentAssembly>
      < assemblyIdentity名称=System.Web.Helpers公钥=31bf3856ad364e35/>
      < bindingRedirect oldVersion =1.0.0.0-2.0.0.0NEWVERSION =2.0.0.0/>
    < / dependentAssembly>
    < dependentAssembly>
      < assemblyIdentity名称=System.Web.Mvc公钥=31bf3856ad364e35/>
      < bindingRedirect oldVersion =1.0.0.0-4.0.0.0NEWVERSION =4.0.0.0/>
    < / dependentAssembly>
    < dependentAssembly>
      < assemblyIdentity名称=System.Web.WebPages公钥=31bf3856ad364e35/>
      < bindingRedirect oldVersion =1.0.0.0-2.0.0.0NEWVERSION =2.0.0.0/>
    < / dependentAssembly>
  < / assemblyBinding>
< /运行>


解决方案

我能够重新您的问题,并找到了解决办法。至于为什么这个解决方案的工作,我不能说,但这里是我遵循的步骤。

1,从一个新的MVC 4项目(基本),我创建了下面的标记了(注意ReSharper的7没有怨言)

视图

  @ {
    ViewBag.Title =指数;
    ViewBag.Array =新的字符串[] {苹果,橙子,香蕉};
}<脚本类型=文/ JavaScript的>
    VAR的东西=!@ Html.Raw(Json.En code(ViewBag.Array));
    警报(东西);
< / SCRIPT>< H2>指数< / H>

2.Compiled和运行应用程序,收到以下错误: CS0103:Json的'这个名字并不在当前的背景下存在

3.Attempted在查看导入的命名空间,添加组件浏览web.config文件,确保项目适当的参考。这些都不了影响。

4 解决方案添加以下行(<&集会GT;
        <添加组件=System.Web.Helpers,版本= 2.0.0.0,文化=中性公钥= 31BF3856AD364E35/>
      < /组件>
)我在编译部分web.config中(主要web.config中,而不是views文件夹一个)

 <&的System.Web GT;
    <的httpRuntime targetFramework =4.5/>
    <编译调试=真targetFramework =4.5>
      <! - 添加以下内容。确保编译不自闭,
          它是在我的 - >
      <&集会GT;
        <添加组件=System.Web.Helpers,版本= 2.0.0.0,文化=中性公钥= 31BF3856AD364E35/>
      < /组件>
    < /编译>

5.Recompile这里是生成的HTML页面输出SANS误差

 < HTML和GT;
< HEAD>
    <间的charset =UTF-8/>
    < META NAME =视口CONTENT =WIDTH =设备宽度/>
    <标题>指数< /标题>
    <链接HREF =/内容/的site.css的rel =stylesheet属性/>    <脚本的src =/脚本/ Modernizr的-2.6.2.js>< / SCRIPT>< /头>
<身体GT;
<脚本类型=文/ JavaScript的>
    !VAR东西= [苹果,橙子,香蕉];
    警报(东西);
< / SCRIPT>< H2>指数< / H>
    <脚本的src =/脚本/ jQuery的-1.8.2.js>< / SCRIPT>
< /身体GT;
< / HTML>

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>

解决方案

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>

这篇关于在CS文件,但不是在CSHTML Json.En code工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆