C# 编译器可以';在Orchard主题中找不到html扩展方法

C# 编译器可以';在Orchard主题中找不到html扩展方法,c#,asp.net-mvc,razor,orchardcms,C#,Asp.net Mvc,Razor,Orchardcms,我正在处理一个Orchard主题,并创建了以下Html扩展方法: using System; using System.Web.Mvc; namespace Themes.MyTheme { public static class HtmlExtensions { public static bool IsCurrent( this HtmlHelper html, string actionName,

我正在处理一个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?)
(和其他人)让我相信我必须将名称空间添加到我的视图文件夹中的web.config,我已经这样做了。但我还是犯了这个错误

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


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

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

/CreateProject:true

除了Html.ActionLink行末尾缺少的分号之外,它对我也有用。我刚刚创建了一个新主题,添加了您的类,并在视图中使用了它。也许你错过了使用结尾的分号。如果您正在使用多个用户,那么这是必要的。(我不必添加对Web.Config的任何引用。)以防万一,因为这实际上比看起来更容易被忽略:您将文件添加到项目中了吗?我添加了分号,但没有帮助。该文件是项目的一部分(尽管这些问题通常都是小错误)。有趣的是,VS2015在我添加using时不会显示曲线,但在我删除它时会显示曲线。所以对于编辑来说,一切都很好。只有在运行时我才有这个问题。我现在已经通过使用剃须刀功能修复了它,但我将尝试更深入地研究它。也许我的果园安装有点可疑。你把这个主题作为一个项目创建了吗?如果不是,这可能是原因。是的,我想应该是这样的。我的主题现在在主题项目下。我必须迁移它。仍然必须添加web.config,但这确实起到了作用。