【ASP.NET】HTMLエンコード Encode ・デコード Decode

エンコード Encode

Server.HTMLEncode()

http://www.site-cooler.com/kwl/asp/13.htm

HttpUtility.HtmlEncode()

 * ASPプロジェクト内でなくても使える
http://msdn.microsoft.com/ja-jp/library/73z22y6h.aspx
http://atamoco.boy.jp/asp/20110116_1.php

サンプル

Public Class HeaderCreater
    Private Const NewLine As String = "<br />"
    Private Const DelimitterColon As String = ":"
    Private Const HeaderFormat As String = "{0}{1}{2}<br />"

    Private _Item As String
    Private _Content As String
    Private _Delimitter As String

    'プロパティ
    Public Property Item As String
        Get
            Return Me._Item
        End Get
        Set(ByVal value As String)
            Me._Item = value
        End Set
    End Property

    Public Property Content As String
        Get
            Return Me._Content
        End Get
        Set(ByVal value As String)
            Me._Content = value
        End Set
    End Property

    Public Property Delimitter As String
        Get
            Return Me._Delimitter
        End Get
        Set(ByVal value As String)
            Me._Delimitter = value
        End Set
    End Property

    Public Sub New( _
        ByVal item As String, _
        ByVal content As String)

        Me.New(item, content, HeaderCreater.DelimitterColon)
    End Sub

    Public Sub New(
        ByVal item As String, _
        ByVal content As String, _
        ByVal delimitter As String)

        Me._Item = item
        Me._Content = content
        Me._Delimitter = delimitter
    End Sub

    Public Function CreateHeader( _
        ByVal headers As IList(Of HeaderCreater)) As String

        Dim returnValue As StringBuilder = New StringBuilder()

        For Each header In headers
            returnValue.Append( _
                Me.CreateHeader(header.Item, header.Content))
        Next

        Return returnValue.ToString()
    End Function

    Public Function CreateHeader( _
        ByVal item As String, ByVal content As String) As String

        If Not String.IsNullOrEmpty(item) AndAlso _
            Not String.IsNullOrEmpty(content) Then
            Return String.Format(HeaderCreater.HeaderFormat, _
                                 item, Me._Delimitter, content)
        ElseIf Not String.IsNullOrEmpty(item) AndAlso _
            String.IsNullOrEmpty(content) Then
            Return item & HeaderCreater.NewLine
        ElseIf String.IsNullOrEmpty(item) AndAlso _
            Not String.IsNullOrEmpty(content) Then
            Return HttpUtility.HtmlEncode(content) & HeaderCreater.NewLine
        Else
            Return String.Empty
        End If
    End Function

End Class