Monday, March 26, 2012

web.config not importing namespaces

I am trying to get every page I have to import the namespaces listed in my web.config file. But for some reason, its not working.

<system.web> <pages> <namespaces> <clear /> <add namespace="System"/> <add namespace="System.Collections"/> <add namespace="System.Collections.Specialized"/> <add namespace="System.Configuration"/> <add namespace="System.Data"/> <add namespace="System.Data.SqlClient"/> <add namespace="System.Net.Mail"/> <add namespace="System.Text"/> <add namespace="System.Text.RegularExpressions"/> <add namespace="System.Web"/> <add namespace="System.Web.Configuration"/> <add namespace="System.Web.UI"/> <add namespace="System.Web.UI.WebControls"/> <add namespace="System.Web.UI.WebControls.WebParts"/> </namespaces> </pages></system.web>
Are you trying to use this in code-behind files? The above imports only work in the ASPX file, not the code-behind.

yep, code behind pages. Is there anyway to do something similar with code behinde?


I don't think there is a global configuration way to declare the namespace declaratively. However, you can create a base class that inherits from Page class, import all your classes there and then every code behind inherits that in turn. (Though, I wouldn't do that unless I'm absolutly sure that every codebehind will be using certain classes)

0 comments:

Post a Comment