What's the point of the Web.config's system.web/Pages/Namespaces Tag?
Asked Answered
S

3

10

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!

Steakhouse answered 29/3, 2012 at 15:48 Comment(2)
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...Homothallic
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.Steakhouse
S
9

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.

Stipendiary answered 29/3, 2012 at 15:54 Comment(2)
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.Lafrance
@Lafrance - 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.Stipendiary
L
7

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.

Logion answered 29/3, 2012 at 15:53 Comment(1)
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.Labyrinth
T
5

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.

Tablet answered 1/2, 2013 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.