January 7, 2010

Why am I suddenly getting type or namespace errors in my ASP.NET web application

I just spent the last 20 minutes bashing my head against this problem, and I know that at some point it's going to come back and bite me again and I'm not going to remember how I fixed it, so this blog post is really just a reminder to my future self that this is how I fixed it - hopefully it will help others too.

I just added files from our source code repository that were contained within a folder within my APP_CODE directory. No sooner as I added them and I started getting the following error at every line that referenced objects contained in other files that resided directly in my APP_CODE directory.

"The type or namespace name could not be found are you missing a using directive"

I immediately checked that the namespaces were referenced correctly - and they were. I checked that the classes I were referencing were marked as public and that I could access them directly, and indeed, they were and I should.

My colleague @JohnMacIntyre (in a rare moment of brilliance) told me to check the web.config for the compilation settings. Sure enough I found:

<compilation debug="true">
        <add directoryName="helpers"/>
        <add assembly="System.Transactions, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Core, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Xml.Linq, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.DataSetExtensions, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

I removed the <codeSubDirectories> section and hey presto, suddenly everything works again. The order of compilation is top-down with the APP_CODE directory being compiled last consequently if you have multiple folders defined here, code in earlier folders cannot be 'seen' by code in subsequent folders but not the other way around and none of the code in these folders can see code in your APP_CODE folder, however code in your APP_CODE folder can see all the code in those folders.


  1. Ben, your comment system sucks.

  2. Thanks for your concern. Please take it up with the venerable Blogger designers at Google... :)

  3. I'd have done just that if I hadn't already used my my *rare* moment of brilliance ....

    ... jackass