Here’s a very useful new feature in ASP.NET 2.0 which allows you to specify the default namespaces that ASP.NET looks for in ASPX pages. In ASP.NET 1.x you either had to use an intrusive @Import directive:
<%@ import namespace="Westwind.Tools" %>
In ASP.NET 2.0 you can now add default namespaces into your web.Config files like this:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web>
<pages>
<namespaces>
<add namespace ="System.IO" />
<add namespace="System.Text"/>
<add namespace="Westwind.Tools"/>
</namespaces>
</pages>
</configuration>
</system.web>
With this setting in place you can now reference any classes in those namespaces directly inside of your ASPX pages. Note that this works only for the ASPX pages themselves, not the code behind partial class code, where you still have to declare the namespaces explicitly. Essentially the ASP.NET page compiler automatically injects these namespace declarations into the generated ASPX class code.
Along the same lines you can add default control libraries to the Pages section of the config file to remove control library references like these from pages:
<%@Register TagPrefix="ww" Namespace="Westwind.Web.Controls" Assembly="wwWebControls" %>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web>
<pages>
<namespaces>
<add namespace ="System.IO" />
<add namespace="System.Text"/>
<add namespace="Westwind.Tools"/>
</namespaces>
<controls>
<add tagPrefix="ww" namespace="Westwind.Web.Controls"
assembly="wwWebControls" />
</controls>
</pages>
</configuration>
</system.web>
As soon as you do this you can type <ww: in an ASPX page and get full intellisense support for your custom controls.
Both of these settings are very welcome in my book as it removes page setup that you’d normally have to do manually and lets you work more easily in HTMLSource only mode without having to resort to design mode.
The new IntelliSense improvements in VS.NET 2005 are really quite impressive in terms of how changes are recognized almost immediately even if they involve configuration settings in web.config. Behind the scenes VS.NET must be compiling the application (or studiously parsing configurations and source code) to make this information available.
While I would argue that this sort of support should have been there in V1, it’s good to see that it’s finally here – it certainly will improve my productivity considerably in getting pages designed which is still the biggest bottleneck to building Web applications efficiently.
As an aside the hierarchy of ASP.NET configuration files has also changed a bit: In 1.1 all the System.Web configuration settings were stored in the machine’s Machine.Config file in the D:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG path. In 2.0 the web specific settings have been extracted out into a separate web.config file which makes finding relative settings a bit easier than in the single massive machine.config file.
Other Posts you might also like