Search code examples
c#asp.net-mvcrazororchardcms

Compiler can't find html extension method in Orchard theme


I'm working on an Orchard theme and have created the following Html extension method:

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;
        }
    }
}

In a Razor view, I've then added this:

@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 { })
}

I am getting the following exception:

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

This answer (and others) led me to believe I had to add the namespace to the web.config in my Views folder, which I have done. But I still get this error.

The extension method is defined in the same assembly as the Razor view (the Themes project in a standard Orchard solution; I created the Theme using codegen and it added the theme there).

How can I get Razor/Orchard/the compiler to recognize this namespace?


Solution

  • You will need to create your theme as its own project, not just a folder under themes. Themes without their own .csproj file cant have code in them I'm afraid. When creating a theme with codegen there is a flag:

    /CreateProject:true