【问题标题】:Compiler can't find html extension method in Orchard theme编译器在 Orchard 主题中找不到 html 扩展方法
【发布时间】:2017-03-28 06:32:24
【问题描述】:

我正在开发一个 Orchard 主题并创建了以下 Html 扩展方法:

using System;
using System.Web.Mvc;

namespace Themes.MyTheme {
    public static class HtmlExtensions {
        public static bool IsCurrent(
            this HtmlHelper html,
            string actionName,
            string controllerName) {

            string contextAction = (string) html.ViewContext.RouteData.Values["action"];
            string contextController = (string) html.ViewContext.RouteData.Values["controller"];

            bool isCurrent =
                string.Equals(contextAction, actionName, StringComparison.CurrentCultureIgnoreCase)
                && string.Equals(contextController, controllerName, StringComparison.CurrentCultureIgnoreCase);

            return isCurrent;
        }
    }
}

在 Razor 视图中,我添加了以下内容:

@using Themes.MyTheme

// More razor syntax

@if (!Html.IsCurrent("New", "Customer")) {
    Html.ActionLink(T("New Customer").ToString(), "New", new { Controller = "Customer", Area = "My.Area" }, new { })
}

我收到以下异常:

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

This answer(和其他人)让我相信我必须将命名空间添加到我的 Views 文件夹中的 web.config 中,我已经这样做了。但我仍然收到此错误。

扩展方法定义在与 Razor 视图相同的程序集中(标准 Orchard 解决方案中的主题项目;我使用 codegen 创建了主题,并在那里添加了主题)。

如何让 Razor/Orchard/编译器识别这个命名空间?

【问题讨论】:

  • 除了 Html.ActionLink 行末尾缺少的分号之外,它对我有用。我刚刚创建了一个新主题,添加了您的类,并在视图中使用了它。也许你错过了使用结束时的分号。如果您使用多个用途,则有必要。 (我不必添加对 Web.Config 的任何引用。)
  • 以防万一,因为这实际上比看起来更容易错过:您是否将文件添加到项目中?
  • 我添加了分号,但没有帮助。该文件是项目的一部分(尽管确实这些问题通常是这些小错误)。有趣的是,当我添加 using 时,VS2015 不会显示波浪线,但当我删除它时会显示。所以对于编辑来说,一切都很好。只有在运行时我才有这个问题。我现在已经通过使用 Razor 功能修复了它,但我会尝试更多地研究它。我安装的 Orchard 可能有些可疑。
  • 您是否将主题创建为项目?如果不是,这可能是原因。

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


【解决方案1】:

您需要将主题创建为自己的项目,而不仅仅是主题下的文件夹。恐怕没有自己的 .csproj 文件的主题不能在其中包含代码。使用 codegen 创建主题时有一个标志:

/CreateProject:true

【讨论】:

  • 是的,我认为它会是这样的。我的主题现在在主题项目下。我必须迁移它。
  • 仍然必须添加 web.config,但这确实可以解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 2022-07-16
相关资源
最近更新 更多