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!
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With