Razor can not find my HTML helper
It needs to be added to the config file in the Views folder, not the main config.
See this answer:
How do I import a namespace in Razor View Page?
Razor HtmlHelper Extensions (or other namespaces for views) Not Found
Since the Beta, Razor uses a different config section for globally defining namespace imports. In your Views\Web.config
file you should add the following:
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<!-- Your namespace here -->
</namespaces>
</pages>
</system.web.webPages.razor>
Use the MVC 3 upgrade tool to automatically ensure you have the right config values.
Note that you might need to close and reopen the file for the changes to be picked up by the editor.
Why can't Razor find my HTML Helper?
Have you added the helper's namespace to your Views/web.config?
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="CUSTOM_NAMESPACE" />
</namespaces>
</pages>
</system.web.webPages.razor>
The above will only work if you're using an RC, if you're on an early Beta, you'll need to add the namespace in the page or Global.asax.
Also, I'd suggest changing the return type to HtmlString
.
return new HtmlString(STRING_VALUE);
System.Web.Webpages.Html.Htmlhelper' does not contain a definition for 'Sitecore'
I have now resolved this issue. I simply had to install Update 2 of Visual Studio 2013. How frustrating.
Thanks to StriplingWarrior and Ahmed Okour for your useful advice.
Related Topics
Setting Unique Constraint with Fluent API
Serialize Property, But Do Not Deserialize Property in JSON.Net
How to Select Xml Nodes with Xml Namespaces from an Xmldocument
How to Fix Error: "Could Not Find Schema Information for the Attribute/Element" by Creating Schema
Auto Create Database in Entity Framework Core
How to Share Data Between Forms
Datatrigger Does Not Change Text Property
Does Distinct() Method Keep Original Ordering of Sequence Intact
How to Make a Property Protected and Internal in C#
Additional Text Encountered After Finished Reading JSON Content:
Is Bcrypt a Good Hashing Algorithm to Use in C#? How to Find It
Why Is Typedreference Behind the Scenes? It's So Fast and Safe... Almost Magical!
Why Datetime.Addhours Doesn't Seem to Work
How to Read Regex Captures in C#