How to add namespaces in web.config file?

38,535

Solution 1

The purpose of the namespace section is to get around having to do the import in the .aspx page. Code behind in C# still requires you to have the using statements at the top of your .cs file.

There is no way to get around this.

Solution 2

You need to put them in the correct <system.web> section. e.g.:

<configuration>
  <system.web>
    <pages>
      <namespaces>
        <add namespace="System.Data" />
        <add namespace="System.Text"/>
      </namespaces>
    </pages>  
  </system.web>
</configuration>

and put them in the correct web.config

i.e. the second web.config file is the Views folder and is specific to views. These settings do not go in the root web.config.

The purpose of these settings is to make the libraries available to the ASPX pages (e.g. for Intellisense) and it is not used for the code-behind. You still need to have using statements in your actual code as that is just plain c# programming.

Share:
38,535

Related videos on Youtube

jams
Author by

jams

Updated on March 23, 2020

Comments

  • jams
    jams about 4 years

    I am using VS 2008 and C# but when I added namespace in web.config file, that namespace is not imported or included in code behind or aspx
    I have also read this question but not get the required answer.

    web.config code

    <configuration>
     <system.web>
        <pages>
          <namespaces>
            <add namespace="System.Data" />
            <add namespace="System.Text"/>
          </namespaces>
        </pages>  
      </system.web>
    </configuration>
    
  • Gone Coding
    Gone Coding over 12 years
    Are you actually expecting them to appear in the text of your ASPX files? If so that is not their purpose. With the config settings you no longer need to have includes for those DLLs in your ASPX files (just in the code behind).
  • jams
    jams over 12 years
    @Chris Lively: Then what is the purpose adding namespace it in web.config file?
  • Oded
    Oded over 12 years
    @jams - If you need to add the namespaces to every page, this will import them automatically. No need for extra imports.
  • jams
    jams over 12 years
    @Oded:That is the problem it is not importing on code behind.
  • Oded
    Oded over 12 years
    @jams - You might be re-importing existing adds. Did you try adding a <clear /> element before the adds?
  • jams
    jams over 12 years
    @Oded:I did exactly but no success. Have you tried it on your system?
  • NotMe
    NotMe over 12 years
    @Oded: it doesn't work in c# code behind files. You have to include the imports line.
  • Oded
    Oded over 12 years
    @ChrisLively - It's not supposed to. It is a substitution for @ import directives (which only apply to the .aspx page, not the code behind). See the documentation.
  • NotMe
    NotMe over 12 years
    @Oded - Yeah, I understand that. The OP doesn't. He wants it to work for code behind.
  • Gone Coding
    Gone Coding almost 10 years
    Surely you meant using statements and not the imports directive for a cs file :)
  • NotMe
    NotMe almost 10 years
    @TrueBlueAussie: Well... yes that would be the correct term.. ;)