Monday, June 17, 2013

MVC: Intellisense for custom HtmlHelpers or if you want to avoid @using to import namespaces

If you write custom HtmlHelpers its quite tedious to have to add @using your.namespace.to.your.htmlHelpers on top of each page. To be able to use your newly written htmlhelpers

to avoid this just find the web.config in your views folder and locate the following section:
<system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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.Optimization"/>
                <add namespace="System.Web.Routing" />
                <add namespace="SharpSolutions..MvcApplication.Helpers.BootStrap"/>
                <add namespace="SharpSolutions.MvcApplication.Helpers"/>
            </namespaces>
        </pages>
    </system.web.webPages.razor>

and add your custom namespace to it.

After you have done this reload your mvc project (or close Visual Studio and reopen it).