Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config


:P
On this page:

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.


The Voices of Reason


 

Bob Archer
June 06, 2005

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Rick,

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

Rick Strahl
June 06, 2005

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

I've noticed that there is a bit of lag in the editor especially in some larger projects that I've imported. Not too bad and to be expected to some degree in a beta.

+++ Rick ---

OdeToCode Links
June 12, 2005

# OdeToCode Links For June 12


Pete
June 28, 2005

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Not sure if this is the right spot for this. But when I try your syntax I get the following error:
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

Rick Strahl
June 28, 2005

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Pete,

Are you sure you're using ASP.NET 2.0 in your site? You'll get this error in a 1.x site.

Mick
April 19, 2006

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Hey Rick,

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

Rick Strahl
April 19, 2006

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Mick, there shouldn't be any performance issues. It's just injecting namespaces and assembly references that you'd need to include in your project anyway. So I'd say not enough of an impact to matter.

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.

Shawn
May 26, 2006

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Hi, wanna check with you, in Visual Studio 2005 i realised that some libraries are not there as stated in Pro asp.net 2.0(reference book). Do I have to subscribe to msdn to make use of the libraries. Please help i'm new to this.

Andrew Robinson
June 06, 2006

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Rick, Have you been able to use this to register usercontrols? I can't seem to get it to work and not finding a lot of info out there?

<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

IceShock
August 31, 2006

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Andrew, one limitation of using this way to register controls for all the pages is that you need to put the WebUserControl in a subfolder, the user control and the pages that use it must not be in the same folder for this to work.

Wayne
August 31, 2006

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Hi Rick

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.

ryan turner
October 19, 2006

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

To register a local control ASP.NET 2.0 allows you to specify the controls within the default namespace as such:
<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

# DotNetSlackers: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config


Amit Patriwala
October 10, 2008

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

hello Rick,
i have add the namespace "System" it is not working. other namespace is working fine.can you suggest how to do that thing?


Thnx

Pankaj
October 22, 2008

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Hi I Add Namespaces In Web.Config. It Work Only For .aspx File But Not For My .cs(Code Behind File) .Is it possble .

pandu
December 28, 2008

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

I added the reference in Web.Config file and it worked for my code behind i.e. .vb. So it should work for .cs as well.

Harmen
January 15, 2009

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Is the following valid? Different assemblies, same namespace:

<add assembly="asm1" namespace="mynamespace" tagPrefix="cc1"/>
<add assembly="asm2" namespace="mynamespace" tagPrefix="cc1"/>

shobin mathew
April 07, 2009

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

it isnot working for me when i use it inside <location> tags

Bujjiamma
July 09, 2009

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Thanks a lot..........

Nilesh Raut
August 03, 2009

# re: Adding default Namespaces and Control libraries in ASP.NET 2.0 with web.config

Thanks a lot

for

<%@ import namespace="Westwind.Tools" %>

Nimesh
August 16, 2009

# re: Data Access Object in .NET

Hello All,

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.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024