Tag mapping in ASP.NET

by Mads Kristensen 10. October 2007 04:03

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.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags: ,

ASP.NET | Tips and tricks

Comments

10/10/2007 4:35:26 AM #

Josh Stodola

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!

Josh Stodola United States |

10/10/2007 5:41:52 AM #

trackback

Trackback from DotNetKicks.com

Tag mapping in ASP.NET

DotNetKicks.com |

10/10/2007 5:42:00 AM #

Brian

Great find, Mads. Thanks for the heads up.

Brian United States |

10/10/2007 6:13:08 AM #

Tom

Wow, Thanks.  That's crazy...

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

Tom United States |

10/10/2007 8:50:25 AM #

Denny Ferrassoli

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

Denny Ferrassoli United States |

10/10/2007 6:22:56 PM #

George Chatzimanolis

WOW this is AMAZING...

George Chatzimanolis Greece |

10/10/2007 7:27:44 PM #

Nebbercracker

Tres bien!!  

Nebbercracker United States |

10/10/2007 9:35:02 PM #

Harsha

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

Harsha United States |

10/10/2007 11:17:49 PM #

pingback

Pingback from mhinze.com

11 Links Today (2007-10-10)

mhinze.com |

10/10/2007 11:45:38 PM #

Joe

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

Joe United States |

10/11/2007 12:01:16 AM #

Josh Stodola

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

Josh Stodola United States |

10/11/2007 12:04:44 AM #

Mads Kristensen

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

Mads Kristensen Denmark |

10/11/2007 12:59:43 AM #

Juan

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)

Juan Argentina |

10/11/2007 1:42:24 AM #

Mads Kristensen

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

Mads Kristensen Denmark |

10/12/2007 12:06:44 AM #

Roman Clarkson

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

Roman Clarkson United States |

10/13/2007 9:55:27 PM #

MK2

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

MK2 People's Republic of China |

10/14/2007 4:20:51 AM #

ajdotnet

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 ... />.

ajdotnet |

10/16/2007 6:13:19 AM #

Amr

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

Amr Egypt |

10/16/2007 9:18:12 AM #

MK2

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

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

MK2 People's Republic of China |

10/23/2007 10:30:12 AM #

trackback

Trackback from The Blog of Josh Stodola

I learned from a post by Mads Kristensen

The Blog of Josh Stodola |

10/26/2007 5:06:45 AM #

njappboy

MSDN info on tagMapping

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

njappboy United States |

3/17/2008 8:12:12 PM #

pingback

Pingback from fireyang.v255.com

FireYang’s Blog » ASP.NET中的标签映射(Tag mapping )(译)

fireyang.v255.com |

6/19/2008 5:50:52 PM #

cxfx

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

cxfx Australia |

6/19/2008 5:59:18 PM #

Mads Kristensen

@cxfx,

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

Mads Kristensen United States |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008