Ever wanted to use your own custom
HtmlHelper extensions inside your NHaml views? I wanted that, but I got this error:
error CS1061: ‘System.Web.Mvc.HtmlHelper’ does not contain a definition for ‘WhyOhWhy’ and no extension method ‘WhyOhWhy’ accepting a first argument of type ‘System.Web.Mvc.HtmlHelper’ could be found (are you missing a using directive or an assembly reference?)
The problem was that NHaml would not know that there existed some extensions of the
HtmlHelper class in some obscure assembly somewhere inside my web app. To do this, you need to tell NHaml exactly which assembly and which namespaces to include in its search. Here is how to do it:
In your web.config, beneath the
<configSections> node, add this
<section name="nhamlViewEngine" type="MvcContrib.NHamlViewEngine.Configuration.NHamlViewEngineSection, MvcContrib.NHamlViewEngine" />
– and then outside of
<configSections>, add this:
This will cause NHaml.config to be included. We could just have embedded the configuration inside the web.config file, but we want to make separate NHaml.config files for each environment we want to deploy our application in. Then we can turn on NHaml’s compiled view caching feature by flicking the production attribute.
An example NHaml configuration is something like this:
<add assembly="MyAssembly" />
<add namespace="MyAssembly.HtmlHelperExtensions" />
<add namespace="MyAssembly.EvenMoreHtmlHelperExtensions" />
Suddenly you will gain access to everything inside the specified namespaces inside your NHaml templates. Nifty!