0

If I have a

using MyProject.language;

literary needs to place on every single page.

My question is, is there a place that I can place the "using" just once, for every page of the project.

Thanks.

H H
  • 263,252
  • 30
  • 330
  • 514
Eric Yin
  • 8,737
  • 19
  • 77
  • 118
  • 2
    When you say "page," is this an ASP.NET project? This inclusion IS possible in ASP.NET projects inside your web.config file, but not on Forms applications. – lsuarez Oct 31 '11 at 20:59
  • 2
    No, you have to use that on every file or part of the project/solution where you need to use classes/enums contained in `MyProject.language` – Marco Oct 31 '11 at 21:00
  • Related: http://stackoverflow.com/questions/789239/does-c-sharp-support-project-wide-default-namespace-imports-like-vb-net – Dave Mateer Oct 31 '11 at 21:05
  • @lthibodeaux, yes it's ASP.net project, I found web.config has system.web -> pages -> namespaces -> "" . I tired, not working. Any idea – Eric Yin Oct 31 '11 at 21:07
  • Please be aware that this only includes the namespace for page directives (inside <% %> tags). It will not make the namespace available to code-behind for that page. Is that where your namespace import is not working? – lsuarez Oct 31 '11 at 21:09
  • Related: http://stackoverflow.com/q/7955762/60761 – H H Oct 31 '11 at 21:21

3 Answers3

3

Using statements have to be in the file where you are actually using the namespace. There is no such thing as a one-place-for-all-usings in C#, unlike C++. The only 'exception' to this are ASP / ASP-MVC files, which automatically add all the using statements defined in the web.config namespaces node. C# / .cs files do not make use of this facility however and still need every namespace manually defined.

Johannes Kommer
  • 6,401
  • 1
  • 39
  • 45
2

Since you mentioned page, you can configure ASP.NET to reference the namespace everywhere. This only affect the views themselves and not the code behind

<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="Microsoft.Web.Mvc" />
    <add namespace="xVal.Html" />
    <add namespace="Telerik.Web.Mvc.UI" />
  </namespaces>
</pages>

If you use the Razor view engine of ASP.NET MVC, you can add a reference for all views like this

<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" />
    </namespaces>
  </pages>
</system.web.webPages.razor>

If you are in a plain C# project or in the code behind of a page, you should know that any class that is located in a namespace will automatically reference all object in any parent namespace. Assuming a namespace Company.Software.Somethings.Data, any classes in the Data namespace will automatically reference the classes in

  • Company.Software.Somethings
  • Company.Software
  • Company
Pierre-Alain Vigeant
  • 22,635
  • 8
  • 65
  • 101
1

You can not do that, as this works only for one file.

Valdas
  • 1,074
  • 13
  • 20