【问题标题】:NancyFx cannot find NewtonSoft reference in Razor View EngineNancyFx 在 Razor 视图引擎中找不到 NewtonSoft 参考
【发布时间】:2014-07-10 18:03:14
【问题描述】:

我有一个简单的部分:

<div class="col-xs-12">
    <label>view model</label>
    <textarea class="form-control btn-xs" rows="12">@JsonConvert.SerializeObject(Model)</textarea>
</div>

以下方法无效:

@using Newtonsoft.Json

在部分或主要的 cshtml 中,也不是:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="Newtonsoft.Json" />
        </namespaces>
    </pages>
</system.web.webPages.razor>

在视图/Web.config 中。我仍然得到:

The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)

The name 'JsonConvert' does not exist in the current context

【问题讨论】:

  • 你真的安装了 Newtonsoft 软件包吗?
  • 我用谷歌搜索了一下,好像有一个特定的包给南希。应该是你的问题。看这里nuget.org/packages/Nancy.Serialization.JsonNet/0.22.2
  • 我已经安装了包,它可以在我的视图之外的项目中使用。

标签: c# asp.net-mvc razor json.net nancy


【解决方案1】:

我必须将以下内容添加到我的 Web.config 以消除此错误

<configuration>
  <configSections>
    <section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" />
  </configSections>

  <razor disableAutoIncludeModelNamespace="false">
    <assemblies>
      <add assembly="Newtonsoft.Json" />
    </assemblies>
    <namespaces>
      <add namespace="Newtonsoft.Json" />
    </namespaces>
  </razor>
</configuration>

【讨论】:

    【解决方案2】:

    愚蠢的问题:您是否将 Json.Net nuget 包添加到您的项目中?

    最后一次尝试不起作用的最简单解释就是这样。

    【讨论】:

    • 是的,我安装了正确的软件包。对 Newtonsoft.Json 的引用在视图之外可以正常工作。
    猜你喜欢
    • 2017-10-15
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多