WinRT (Metro)– Extension Methods to Encode/Decode HTML and URL’s

Posted: 08/31/2012

Here are some String extension methods to handle Encoding/Decoding HTML and Encoding/Decoding URL’s. I use these a lot and the location seems to change based on what flavor of the framework you’re using and where. Having it as a String extension ensures I usually only have to remember that it exists as a string extension and for forgetful people like me, that saves time. :)

VB.Net

        ''' <summary>
        ''' HTML Encodes a string.
        ''' </summary>
        ''' <param name="buf"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> _
        Public Function HtmlEncode(buf As String) As String
            Return System.Net.WebUtility.HtmlEncode(buf)
        End Function

        ''' <summary>
        ''' HTML Decodes a string.
        ''' </summary>
        ''' <param name="buf"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> _
        Public Function HtmlDecode(buf As String) As String
            Return System.Net.WebUtility.HtmlDecode(buf)
        End Function

        ''' <summary>
        ''' Encodes text for use in a URL.
        ''' </summary>
        ''' <param name="urlText"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> _
        Public Function UrlEncode(urlText As String) As String
            Return System.Net.WebUtility.UrlEncode(urlText)
        End Function

        ''' <summary>
        ''' Decodes text for use in a URL.
        ''' </summary>
        ''' <param name="urlText"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> _
        Public Function UrlDecode(urlText As String) As String
            Return System.Net.WebUtility.UrlDecode(urlText)
        End Function

C# (Via Telerik's Code Converter)

    /// <summary>
    /// HTML Encodes a string.
    /// </summary>
    /// <param name="buf"></param>
    /// <returns></returns>
    /// <remarks></remarks>
    [Extension()]
    public string HtmlEncode(string buf)
    {
        return System.Net.WebUtility.HtmlEncode(buf);
    }
    /// <summary>
    /// HTML Decodes a string.
    /// </summary>
    /// <param name="buf"></param>
    /// <returns></returns>
    /// <remarks></remarks>
    [Extension()]
    public string HtmlDecode(string buf)
    {
        return System.Net.WebUtility.HtmlDecode(buf);
    }
    /// <summary>
    /// Encodes text for use in a URL.
    /// </summary>
    /// <param name="urlText"></param>
    /// <returns></returns>
    /// <remarks></remarks>
    [Extension()]
    public string UrlEncode(string urlText)
    {
        return System.Net.WebUtility.UrlEncode(urlText);
    }
    /// <summary>
    /// Decodes text for use in a URL.
    /// </summary>
    /// <param name="urlText"></param>
    /// <returns></returns>
    /// <remarks></remarks>
    [Extension()]
    public string UrlDecode(string urlText)
    {
        return System.Net.WebUtility.UrlDecode(urlText);
    }