【问题标题】:Why doesn't the compiler find my razor helpers in an Orchard theme?为什么编译器在 Orchard 主题中找不到我的剃刀助手?
【发布时间】:2014-08-22 01:15:32
【问题描述】:

我想在视图之间共享从 mediaPart 创建 img 标记的逻辑。我已经创建了以下剃须刀helper 方法:

@helper CreateImgFromMediaPart(dynamic mediaPart, int width = 150)
{
    var imgSrc = mediaPart != null ? mediaPart.MediaUrl : string.Empty;
    var imgAlt = mediaPart != null ? mediaPart.AlternateText : string.Empty;
    <img alt="@imgAlt" src="@imgSrc" />
}

我已经通过创建一个 App_Code 文件夹,将 MyHelpers.cshtml 文件放入其中,并将上述帮助方法放入该文件来尝试此操作。不幸的是,我们收到以下错误:

当前上下文中不存在名称“MyHelpers”。

【问题讨论】:

    标签: c# asp.net-mvc razor asp.net-mvc-areas orchardcms-1.8


    【解决方案1】:

    您是否在views 文件夹中添加了对web.config 的dll 引用?这是用于解决剃刀特定的帮助程序/定义的内容。

    【讨论】:

      【解决方案2】:

      Mike Beeler 的回答奏效了。这是我添加到 Views 文件夹中的 web.config。

      <configuration>
        <configSections>
          <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
            <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
          </sectionGroup>
        </configSections>
      
        <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>
              <!-- This is the key line -->
              <add namespace="MyBase.Namespace.Extensions" />
            </namespaces>
          </pages>
        </system.web.webPages.razor>
      </configuration>
      

      【讨论】:

      • 命名空间MyBase.Namespace.Extensions 来自哪里?它是剃刀助手所在的命名空间吗? AFAIK 与 C# 代码不同,您不能将 razor 视图放在命名空间中,不是吗?或者MyBase.Namespace.Extensions项目名称?
      猜你喜欢
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 2018-05-19
      • 2011-05-07
      • 2019-08-01
      • 2012-09-12
      • 2020-02-19
      • 1970-01-01
      相关资源
      最近更新 更多