0

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?

Community
  • 1
  • 1
Peter
  • 13,733
  • 11
  • 75
  • 122
  • Besides the missing semicolon from the end of the Html.ActionLink line it works for me. I just created a new theme, added your class, and used it in a view. Maybe you miss the semicolon from the end of the using. It's necessary if you're using more than one using. (I didn't have to add any reference to Web.Config.) – humanoidcreature Mar 28 '17 at 13:15
  • Just in case, because that's actually easier to miss than it seems: did you add the file to the project? – Bertrand Le Roy Mar 28 '17 at 20:25
  • I've added the semicolons, but it didn't help. The file is part of the project (though indeed these issues are often these small errors). Funny enough, VS2015 doesn't show squiggly lines when I add the using, but does when I remove it. So for the editor, all is fine. It's only at runtime that I have this issue. I've fixed it now by using a Razor function, but I'll try to look into it more. Maybe something fishy with my specific installation of Orchard. – Peter Mar 29 '17 at 06:37
  • Did you create the theme as a project? If not, this might be the cause. – ViRuSTriNiTy Mar 30 '17 at 10:50

1 Answers1

1

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
Hazza
  • 6,441
  • 3
  • 24
  • 37
  • Yeah, I figured it would be something like this. My theme is now under the Themes project. I'll have to migrate that. – Peter Mar 30 '17 at 14:29
  • Still had to add the web.config, but this indeed does the trick. – Peter Mar 30 '17 at 18:45