I’ve got a typical C# automatic property. How can I apply WebUtility.HtmlDecode() when I’ve only got a get; set;?
UPDATE:
Ok, dumb mistake of the day. I had a weird issue where my web.config db connection string was pointed to the right server but for some reason since I had 2 instances (one sql 2008 and 2012) it was still picking up the instance of that DB in 2008 which had the encoding still there. I had fixed the encoding issue by just decoding the Title via a unit test I created in the 2012 DB which in this case this whole fing post was unecessary in stack because the ultimate problem was it was reading from the old DB (messing me up).
Anyway I had already fixed this, finally got rid of the 2008 copy and now it’s reading it fine after my fix:
[Test]
public void CleanAllPostEntries_DecodeHTML_DecodeWasSuccessful()
{
// Arrange
// Act
IEnumerable<Entry> posts = PostCRUD.GetAllPosts();
foreach (Entry post in posts)
{
post.Title = WebUtility.HtmlDecode(post.Title);
post.Body = WebUtility.HtmlDecode(post.Body);
post.MetaTitle = WebUtility.HtmlDecode(post.MetaTitle);
PostCRUD.UpdatePost(post);
//System.Diagnostics.Debug.WriteLine("id: " + post.Id);
//System.Diagnostics.Debug.WriteLine("title: " + WebUtility.HtmlDecode(post.Title));
//System.Diagnostics.Debug.WriteLine("body: " + WebUtility.HtmlDecode(post.Body));
}
//Assert
// TODO: add asserts
}
So I don’t think I need the decode afterall..I already did it!
You really don’t want to do HTML encoding/decoding via properties, although you could if you wanted to. There are several problems with this:
What you really want to do is use a stronger type that represents the HTML-encoded string.
The .NET 4.0 framework includes a
System.Web.HtmlStringtype which you should use for this purpose. In fact, use theSystem.Web.IHtmlStringinterface if you wish to remain general.