1

I'm trying to use TagBuilder to build a tag with multiple classes, but I'm hitting a snag. We use the AntiXssEncoder. When I add multiple classes to TagBuilder, it apparently calls encode on it at some point.

That may work in a default environment, but if the encoder has been replaced with something more stringent, then things like the spaces in my "class" attribute get encoded, etc., which breaks all kinds of things.

Is there any way to turn off attribute encoding for a given TagBuilder instance?

tereško
  • 58,060
  • 25
  • 98
  • 150
Michael Hallock
  • 1,413
  • 1
  • 11
  • 17

1 Answers1

0

It seems to be an issue with .NET 4.0, so if you want to use the TagBuilder, and not just concatenate a string (which of course would maybe be a better idea, if you don't want the default functionality).

However, as is explained in this question, you can create your own override of HttpEncoder, which does not escape the string.

See this SO question and the accepted answer for further details: Stop the tag builder escaping single quotes ASP.NET MVC 2

Community
  • 1
  • 1
Erik A. Brandstadmoen
  • 10,430
  • 2
  • 37
  • 55
  • 1
    I saw that. It's not an exceptable answer... for anyone really, so I can't believe no one has said anything there. That essentially turns off output encoding for EVERYTHING, not just TagBuilder. That's like saying "This lock is annoying so I took off the door". I've worked around it temporarily by just HtmlUnencoding the builder.ToString() before passing it to HtmlString for now. – Michael Hallock Aug 30 '13 at 19:26
  • Haha. Love your analogy :) And, you do have a point. Of course, you could probably add a property to the custom TagBuilder saying not to output encode this particular instance, however, I can't think of the details right now. – Erik A. Brandstadmoen Aug 30 '13 at 19:31