Subscribe

Close

Thank you for visiting!

Please consider subscribing to the RSS feed or following me on Twitter.

Add controls and namespaces to Web.Config for easier maintenance

Very often you add a namespace or register an assembly on top of an ASP.NET page, like this:

<%@ Page Language="C#"
	AutoEventWireup="True"
	CodeBehind="Default.aspx.cs"
	Inherits="ProjectName._Default" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="ProjectName.Classes" %>
<%@ Register Assembly="ProjectName" Namespace="ProjectName.Controls" TagPrefix="cc" %>

It works to do once or twice, but when you need to add these namespaces and controls on several different pages, it quickly becomes maintenance hell.

What to do? Use Web.Config instead.

You just have to declare your namespaces and controls in the system.web section, like this:

<system.web>
	<pages>
		<namespaces>
			<add namespace="System.IO" />
			<add namespace="ProjectName.Classes" />
		</namespaces>
		<controls>
			<add assembly="ProjectName" namespace="ProjectName.Controls" TagPrefix="PN" />
		</controls>
	</pages>
</system.web>

Then you will be able to write code like

<%=Toolbox.FormatHtml("....") %>

instead of code like

<%=ProjectName.Classes.Toolbox.FormatHtml("....") %>

A clear improvement. You also won't need to register your controls everywhere to use them.

If you haven't been using this feature (it already came in ASP.NET 2.0!) it's time now!

Comments

Thanks man its work.

To the top