Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
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
- Adding minimal OWIN Identity Authentication to an Existing ASP.NET MVC Application
- Resolving Paths To Server Relative Paths in .NET Code
- Map Physical Paths with an HttpContext.MapPath() Extension Method in ASP.NET
- Back to Basics: Rewriting a URL in ASP.NET Core
- Getting the Client IP Address in ASP.NET Core
The Voices of Reason
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
+++ Rick ---
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
Unrecognized attribute "xmlns"
If I remove the xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
I get "child nodes are not allowed"
Thanks In advance
Pete
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
Are you sure you're using ASP.NET 2.0 in your site? You'll get this error in a 1.x site.
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
We are looking at doing this for all of our namespaces (100+). Will this cause any performance issues that you know of?
Mick
mforce@handi.com
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
Also remember in ASP.NET 2.0 applications are precompiled (in most cases) so all compilation other than final JITting is already done by the time the code hits the server.
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
<controls>
<add tagPrefix="StoreWeb" tagName="MenuChoiceUserControl" src="MenuChoiceUserControl.ascx"/>
</controls>
and get a Configuration Error: The relative virtual path 'MenuChoiceUserControl.ascx' is not allowed here.
-Andy
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
You mention above that these imports only apply to the aspx page. When I check the code behind page for a C# page, the namespaces are imported automatically. This doesn't occur for VB pages. Do you know why this is and if there is a setting that will also import them directly into the VB page?
Cheers,
Wayne.
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
<controls>
<add src="~/Controls/CompanyNameControl.ascx" tagName="CompanyNameControl" tagPrefix="cnc"/>
</controls>
Slighty different syntax, but accomplishes the same thing... and yes... putting the "~" in front of the path allows nested folders to work correctly.
Best wishes.
>rt
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
i have add the namespace "System" it is not working. other namespace is working fine.can you suggest how to do that thing?
Thnx
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
<add assembly="asm1" namespace="mynamespace" tagPrefix="cc1"/>
<add assembly="asm2" namespace="mynamespace" tagPrefix="cc1"/>
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
for
<%@ import namespace="Westwind.Tools" %>
# re: Data Access Object in .NET
This Article about Data Access Object in .NET. Must read.
How to deal with Data Access Object in .NET Framework, all points explain in this Article.
It's really very userfull.
for further infomration open "http://dotnetheavens.blogspot.com/"
Thanks
-Nimesh.
# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config
Sounds like some cool stuff. Just the other day I needed to create an ASPX page with in line code so it could be shipped as a patch to an existing ap, without having to rebuild the mega dll and such. I didn't even find the @Import thing, just used the full name.
I assume VWD is using a file watcher for the config file. Seems pretty cool how much stuff they can do on the fly. I hope it doesn't slow the UI down the way VB.Net background compile does. Man, I hate opening our huge VB.Net app and having to wait 20 minutes before I can use my PC again.
BOb