1

I would like know if there's any way to avoid using the keyword @using in every page to import some code.

An example: I would like to use @Styles.Render("~/js/jquery-ui") in some pages and I don't want to manually write @using System.Web.Optimization.

I saw in a blog that I can add a namespace in <page> in web.config, but I tried this but it doesn't work.

Is there any way?

StuartLC
  • 104,537
  • 17
  • 209
  • 285
MuriloKunze
  • 15,195
  • 17
  • 54
  • 82

3 Answers3

5

You could add it to the namespaces section of your ~/Views/web.config (not ~/web.config) file:

  <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 other namespaces that you want to be available in scope
            in all your Razor views
      </namespaces>
    </pages>
  </system.web.webPages.razor>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I tried this: but it doesn't work. – MuriloKunze Sep 12 '12 at 16:07
  • 1
    This should work. Make sure you have reopened and closed the view in Visual Studio for changes to take effect. At runtime it will work. You can safely ignore the warnings that Visual Studio might be giving you. – Darin Dimitrov Sep 12 '12 at 16:08
2

Add it to the web.config in the Views folder, under the system.web.webPages.razor element.

<system.web.webPages.razor>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="System.Web.Optimization.Styles" />
    </namespaces>
  </pages>
</system.web.webPages.razor>
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
1

You should be able to add a global namespace in the web.config, as explained here.

Community
  • 1
  • 1
schellack
  • 10,144
  • 1
  • 29
  • 33