我可以将文本 属性 定义为富文本吗?

Can I define a text property as rich text?

VS 2013,VB,EF6

我正在创建一个将用户输入保存在其属性之一中的对象。我希望将用户输入存储为富文本。使存储的文本成为富文本格式涉及什么?所以,

Public Property Text as <what?>

富文本以富文本格式存储。

可在此处找到富文本格式规范:

http://www.microsoft.com/en-us/download/details.aspx?id=10725

这只是一个普通的字符串。您可以使用 SaveFile 函数从 RichTextBox 中提取字符串:

Private Function GetRTF(ByRef Box As RichTextBox) As String
    Using ms As New IO.MemoryStream
        Box.SaveFile(ms, RichTextBoxStreamType.RichText)
        Return System.Text.Encoding.ASCII.GetString(ms.ToArray)
    End Using
End Function

您可以使用 RichTextBox 的 LoadFile 方法将 Rich Text 格式的文本加载到 RichTextBox 中。文本需要采用正确的格式:

Dim rtf As String = "{\rtf1 {\colortbl;\red0\green0\blue255;\red255\green0\blue0;}Guten Tag!\line{\i Dies} ist ein\line formatierter {\b Text}.\line Das {\cf1 Ende}.}"
Using ms As New IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(rtf))
    RichTextBox1.LoadFile(ms, RichTextBoxStreamType.RichText)
End Using

普通控件通常不会在其文本中解释这种格式属性。

我想我会 post 对于可能以与我相同的方式提出问题的其他人,我的回答是什么。我首先说明我的问题格式不正确,因为我不明白我不是真的存储 RTF,我存储的是带有 html 标签的所见即所得文本。但我认为这个问题的措辞很有用,因为这是很多人在被别人教导之前的想法。

最终这个过程会打开一个严重的XSS向量,但首先我们至少要收集所见即所得的文本。

第一步: 使用基于脚本的编辑器捕获带有 html 标签的文本。我使用了 CKEditor which is easy to download on NuGet. It comes in 3 flavors: basic, standard and full. Another popular one seems to be TinyMCE 也可以通过 NuGet 获得。

CKEditor 必须 'wired in' 才能替换现有的输入元素。我直接把@html.editorfor换成一个