views:

248

answers:

2

Is there some sort of magic I need to use to get the namespaces in the pages/namespaces element in the webconfig?

<pages>
    <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="System.Web.WebPages"/>
        <add namespace="System.Web.Helpers"/>
        <add namespace="MyCustomHelpers"/>
    </namespaces>
</pages>

The above just doesn't want to work. I know the namespace is fine because when I put the @using MyCustomHelpers at the top of the page it magically works.

This is so that I can get the Html.SomeFunction() to work without having to put @using at the top of all my pages

+2  A: 

If you put your namespace declaration in the Web.config in the Views folder it should work as expected.

BuildStarted
+10  A: 

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 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.

marcind
FWIW, I've found that the `pageBaseType="System.Web.Mvc.WebViewPage"` attribute is also required. I've updated your answer.
Portman