Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 830
JonathanWolfson Avatar asked Mar 29 '12 15:03

JonathanWolfson


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.

like image 66
Alexei Levenkov Avatar answered Nov 20 '22 03:11

Alexei Levenkov


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.

like image 39
Bradley Mountford Avatar answered Nov 20 '22 05:11

Bradley Mountford


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.

like image 33
Mike Avatar answered Nov 20 '22 04:11

Mike