Tag mapping in ASP.NET

Oct 9, 2007

Recently I started working at ZYB and haven’t seen all the code yet. Then the other day I fell over a special section in our web.config called tagMapping. I’ve never heard about it before so I asked around and did a little detective work. Basically, it’s a way to turn all instances of a type into another type at compile time. In human language it means that it can turn all e.g. System.Web.UI.WebControls.Textbox instances in the entire website into another control.

That is so cool that I had to do a little example. I’ve created a very simple control that inherits from a TextBox and overrides the Text property so that it HTML encodes the text. I placed it in the App_Code folder and called it SafeTextBox.

public class SafeTextBox : System.Web.UI.WebControls.TextBox
{
  public override string Text
  {
    get
    {
      return base.Text;
    }
    set
    {
      base.Text = System.Web.HttpUtility.HtmlEncode(value);
    }
  }
}

Then I needed to hook the tag mapping up in the web.config to convert all the text boxes into SafeTextBox instances. It simply converts all TextBox instances on the entire site. Here is what’s needed in the web.config:


<pages>

<tagMapping>

 <add tagType="System.Web.UI.WebControls.TextBox" mappedTagType="SafeTextBox"/>

</tagMapping>

</pages>

That is one smart way of applying your own server control substitute classes on a site wide basis. I'm still a little frustrated by the fact that I didn't know about this before very recently.

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (20) -

Josh Stodola
Josh Stodola United States
10/9/2007 7:35:26 PM #

AWESOME!  I needed a similiar functionality during an upgrade we had about a month back.  Boy, this could have saved me some time.  I am also frustrated for not knowing about this.  Thanks for sharing, dude!

Brian
Brian United States
10/9/2007 8:42:00 PM #

Great find, Mads. Thanks for the heads up.

Tom
Tom United States
10/9/2007 9:13:08 PM #

Wow, Thanks.  That's crazy...

Honestly I think I'm going to do that in my projects from now on.  

Denny Ferrassoli
Denny Ferrassoli United States
10/9/2007 11:50:25 PM #

That is really cool. Great find!

P.S. BlogEngine.NET has a mention in Scott Gu's latest blog: weblogs.asp.net/.../...ax-silverlight-and-net.aspx

George Chatzimanolis
George Chatzimanolis Greece
10/10/2007 9:22:56 AM #

WOW this is AMAZING...

Nebbercracker
Nebbercracker United States
10/10/2007 10:27:44 AM #

Tres bien!!  

Harsha
Harsha United States
10/10/2007 12:35:02 PM #

we had used this when we were using telerik r.a.d controls. There they were TagMapping Validator's though.

Joe
Joe United States
10/10/2007 2:45:38 PM #

So this is out of the box functionality, or did they write a custom handler to do this?

Josh Stodola
Josh Stodola United States
10/10/2007 3:01:16 PM #

@Joe - This is a built in functionality.  Can you believe it?!

Mads Kristensen
Mads Kristensen Denmark
10/10/2007 3:04:44 PM #

Yes, build into ASP.NET 2.0. It's a two year old feature that just waited to be found.

Juan
Juan Argentina
10/10/2007 3:59:43 PM #

Hi... off topic comment =)

How do you post with the "VS.NET source" style?
(Gray background and colored reserved words, couldn't do it by copy&paste from VS)

Mads Kristensen
Mads Kristensen Denmark
10/10/2007 4:42:24 PM #

@Juan, I use the code syntax highlighter feature of BlogEngine.NET

Roman Clarkson
Roman Clarkson United States
10/11/2007 3:06:44 PM #

There you go again.  I will use this for sure.  

MK2
MK2 People's Republic of China
10/13/2007 12:55:27 PM #

Wow, It just working.....I collect this article. Thanks, Mads.

ajdotnet
ajdotnet
10/13/2007 7:20:51 PM #

The sad part is that it doesn't work at design time. Put an additional property in your class and it won't show up until you place it correctly in your page, rather than relying on the tagMapping entry.
That's why I recently got rid of tagMapping. Rather I derive every control with the same name, different namespace of course. This way I only have to replace the prefix in the html code.

More important (IMHO) is the possibility to pre-announce these controls in the web.config, without having to register them in each web page:
<pages>
    <controls>
        <!-- predefined tag for controls in libraries-->
  <add tagPrefix="sdx" assembly="SDX.WebTemplate.UI.Web.Controls" namespace="SDX.WebTemplate.UI.Web.Controls"/>
  <!-- predefined tag for ~/controls/*.ascx -->
  <add tagPrefix="sdxControls" tagName="banner" src="~/Controls/Banner.ascx" />
    </controls>
...

All I have to do now ist to replace <asp:TextBox ... /> with <sdx:TextBox ... />.

Amr
Amr Egypt
10/15/2007 9:13:19 PM #

Re:@Juan, I use the code syntax highlighter feature of BlogEngine.NET
I dont know how it works or how to use it ? any help please.
regards

MK2
MK2 People's Republic of China
10/16/2007 12:18:12 AM #

Amr:
Just like this:
[code]
//code here
[/code]

If you want to know more, see this: blog.furred.net/.../...ting-for-BlogEnginenet.aspx

njappboy
njappboy United States
10/25/2007 8:06:45 PM #

MSDN info on tagMapping

msdn2.microsoft.com/en-us/library/ms164641.aspx

cxfx
cxfx Australia
6/19/2008 8:50:52 AM #

This does't seem to work with HtmlControls.  Any ideas?

Mads Kristensen
Mads Kristensen United States
6/19/2008 8:59:18 AM #

@cxfx,

You need to update the .NET Framework 2.0 with the service pack to make it work on HtmlControls.

Pingbacks and trackbacks (4)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.