How to add extra namespaces to Razor pages instead of @using declaration?

Update: please take a look at my updated answer that applies to MVC 3 RC: Razor HtmlHelper Extensions (or other namespaces for views) Not Found

This has changed between MVC 3 Preview 1 and MVC 3 Beta (released just today). In Preview 1 Razor used the WebForms namespaces config section. However in the Beta there is a new config section that is seperate from the WebForms one. You will need to add the follwing to your web.config file (or just start with a brand new project from the template):

<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>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="MyCustomHelpers" />
    </namespaces>
  </pages>
</system.web.webPages.razor>

Note that you might need to close and reopen the file for the changes to be picked up by the editor.

Note that there are other changes to what is required in web.config to get Razor to work in MVC3 Beta so you would be best off to take a look at the ~\View\Web.config file that ships in the Beta project templates.

Leave a Comment