Regardless of whether my machines's root web config (the one in Windows/Microsoft.NET/...) contains system.web/pages/namespaces/add
elements, it still is demanded that I include using statements atop each aspx.cs CodeBehind file. Why? Shouldn't it compile and not complain that it cannot understand what a Page is? After all, <add namespace="System.Web" />
exists in the root web.config!

- 861
- 4
- 20
- 30
-
Will it compile with false complaints, or actually won't compile? VS has always struggled with control namespaces added to the webconfig, however in most cases everything builds and runs fine... – KP. Mar 29 '12 at 15:50
-
It does not compile. In a system where server code resides in the CodeBehind file, there truly is zero value to this particular web configuration section. – JonathanWolfson Mar 30 '12 at 19:31
3 Answers
The system.web/pages/namespaces/add
element is for .ASPX
files, normal .CS
files (including .ASPX.CS
) still need to do proper using
as there is no pre-processing of the source before compilation. So .ASPX.CS
must be valid CS file including all using Namespace
declarations.

- 98,904
- 14
- 127
- 179
-
hi, can you explain more about 'pre-processing'. how does the .aspx files interacted with the 'system.web/pages/namespaces'(or [@import] directive) during 'pre-processing'. thanks. – pinopino Jul 14 '14 at 08:43
-
@pinopino - if you have question please ask new one (and if you are asking clarifying question - indeed link to original). I'm not quite sure what do you want to learn about pre-processing in relation to ASP.Net as there is none. You may want to start with [wikipedia - Preprocessor](http://en.wikipedia.org/wiki/Preprocessor_%28programming%29). – Alexei Levenkov Jul 14 '14 at 14:28
This adds namespaces to your aspx and ascx files so that you do not have to include <%@ Import Namespace="MyNameSpace" %>
statements.
From the documentation on MSDN:
The namespaces element defines a collection of import directives to use during assembly pre-compilation. This attribute corresponds to the @ Import directive on an ASP.NET page. The @ Import directive allows you to specify namespaces that are automatically imported into all pages of an application.
Your codebehind is in no way affected by these web.config entries.

- 3,365
- 1
- 42
- 52

- 8,195
- 4
- 41
- 41
-
This is what I have found, regarding VB.NET. In one of my machines I can't build and on another I can. So, I went to about screen and see: This can build: "Microsoft Visual Basic 2010 01019-532-2002102-70462". This can't: "01019-532-2002102-70649". Hope it helps. – Marco Alves Jun 05 '14 at 02:43
The other answers here indicate that this web.config setting only affects the ASPX pages, and not the code-behind pages such as ASPX.CS. I have just tested this in an ASP.NET website targeting .NET 4.0, and have found this to be true for C# pages, but not true for VB pages.
Adding a namespace into the web.config settings removed the requirement to list it at the top of every ASPX.VB page.
I would be interested in any theories why this might be. I would have thought it would work for both ASPX.CS and ASPX.VB code behind files.

- 1,686
- 19
- 14