views:

79

answers:

3

Can I declare namespaces in the web.config so that I don't have to write using statements for each namespace in each of my codebehind files?

A: 
<?xml version="1.0"?>
<configuration>
   <system.web>
      <pages>
         <namespaces>
            <add namespace="MyNamespaces.NamespaceName" />
         </namespaces>
      </pages>
   </system.web>
</configuration>

MyNamespaces.NamespaceName will be now available for all of your pages in project.

cheers

Marko
A: 
<configuration>
  <system.web>
    <pages>
      <namespaces>
        <add namespace="MyNamespace" />
      </namespaces>
    </pages>
  </system.web>
</configuration>

Is this what you're looking for?

Matti Virkkunen
thanx u for reply.if i declared namespace in web.config under pages<?xml version="1.0"?> <configuration> <system.web> <pages> <namespaces> <add namespace="System.Data" /> <add namespace="System.Text" /> </namespaces> </pages> </system.web> </configuration>how to access in .cs file
kumar
without declaring again in .cs file
kumar
@kumar: Oh, this only works for UI code (.ascx files and whatnot) - you'll always need to import the namespaces in .cs files. Automagical importing is a messy VB feature which shouldn't exist anyways, so it's not available for C#.
Matti Virkkunen
ohh.Is it.thanx for reply
kumar
@Matti, post your comment again as an answer and @kumar can accept it.
Greg B
@Greg: This answer was by me...
Matti Virkkunen
@Matti, but you don't explain that it only works for .as*x files in your answer
Greg B
A: 

You can do this for namespaces or common controls:

<system.web>
    <pages>
      <namespaces>
        <add namespace="The.Namespace" />
      </namespaces>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" etc... />
      </controls>
    </pages>
</system.web>
jwsample