Avoid HTML Escaping in an ASP.NET MVC WebGrid Column Format

I ran into an issue with the new ASP.NET MVC WebGrid.  I had difficulty finding any information out there on a good way to do this, so I thought I would post my solution.  The WebGrid easily generates paged and sorted tables automatically. You can find more information on the ASP.NET MVC WebGrid here.  In my case, I am listing some data for the user and I want to show any new rows in bold to make it more noticeable. Adding any HTML tags to the format property as a string will result in the tags being escaped. See the following example:

@grid.GetHtml(
        columns: grid.Columns(
            grid.Column("Text", format: (item) => item.New ? item.Text : "<strong>" + item.Text + "</strong>")
        )
    )

Notice that item.New is a Boolean value.  This results in the following encoded HTML output within the table cell:

<strong>Sample Item Text</strong>

Not exactly the intended effect.  However, if you pass in a new object of type MvcHtmlString with the string in its constructor:

@grid.GetHtml(
        columns: grid.Columns(
            grid.Column("Text", format: (item) => item.New ? item.Text : new MvcHtmlString("<strong>" + item.Text + "</strong>"))
        )
    )

Voila!  This will result in the properly bolded text:

Sample Item Text

This method will work for any of the other Html Helpers as well, though my example specifically references WebGrid.  In other cases, the Html.Raw method can be used to print HTML, but inside of the grid column it will still be escaped.  I’ve tested multiple ways of adding the grid column format using “@:”, but none of the documented methods seem to work within a ternary operator.

BitTorrent Sync and the Future of Peer-to-peer

In recent years, Peer-to-peer networks have matured greatly.  They have most commonly been used as a means to transfer files indirectly without needing a centralized host, but peer-to-peer networks are increasingly being used for more intensive and private processes.  They are many services that use peer-to-peer networks to stream media content.  Spotify uses them to assist their servers to stream music to their users and Skype uses them for video chat.  However, Bitcoin has attracted the most attention to the peer-to-peer evolution.  It’s changing the way we think about currency and the capabilities of secure peer-to-peer data transfers.

The newest addition to the crowd is BitTorrent Sync, a solution for securely sharing and updating files across multiple devices.  BitTorrent Sync places a focus on security; all data transfer is encrypted with a 256-bit AES encryption key, so your files are kept private.  If you want to send files to another user, there is a public key-like process available.  This process relies on keys called “secrets” that are directly linked to the permissions that can be performed by a user on the synced files.  You can provide users with full control, write or read-only access using a “secret” and BitTorrent Sync allows the proper level of control.  You can also

BitTorrent Sync is not a replacement for Dropbox, Google Drive or any other cloud storage.  It only syncs files across your devices; it does not back them up to the cloud.  If you upload a file to cloud storage, it is available whether the devices that you are using are connected or not.  With BitTorrent Sync, the file you need to retrieve would not be available on your laptop if your main PC is shutdown, unless you have it synced to another device.  Still, BitTorrent Sync is a great way to create an automatic offsite backup or sync large files.  It may at some point even rival single use, expiration based file sharing services such as YouSendIt.  Best of all, it won’t cost you or your company a dime.

BitTorrent Sync is currently available on Windows, OS X  and Linux.  One could speculate that iOS and Android versions are likely in the pipeline as well.