关于html解码和编码类型ASP。NET MVC

时间:2022-10-28 16:39:15

I have a snippet <h2>SomeText</h2>

我有一个片段

SomeText

When I saved in database from ckeditor change to it: &lt;h2&gt;SomeText&lt;/h2&gt;

当我在数据库中保存的时候,我就会修改它:<h2>SomeText</ h2gt;

now when I am using @WebUtility.HtmlDecode alone it's not display correctly and change to it: &amp;lt;h2&amp;gt;SomeText&amp;lt;/h2&amp;gt;

当我使用@WebUtility时。单独的HtmlDecode它不能正确显示并更改为:

So When I am using @Html.Raw alone it's not display correctly and chage to it: &lt;h2&gt;SomeText&lt;/h2&gt;

当我使用@Html时。单独使用时,不能正确显示和切换:< SomeText<

Now:

现在:

I can't understand why did not work alone when I am using @Html.Raw or @WebUtility.HtmlDecode? but when I am using First @WebUtility.HtmlDecode and next @Html.Raw together it's work correctly like it. @Html.Raw(WebUtility.HtmlDecode(@Item.Content))

我不明白为什么我在使用@Html时不单独工作。生的或@WebUtility.HtmlDecode吗?但是当我使用@WebUtility时。下@Html HtmlDecode和。生在一起,它的工作是正确的。@Html.Raw(WebUtility.HtmlDecode(@Item.Content))

&lt;h2&gt;SomeText&lt;/h2&gt; Stored in @Item.Content

& lt;h2> SomeText</ h2>存储在@Item.Content

2 个解决方案

#1


1  

Simply use

简单地使用

@Html.Raw("&lt;h2&gt;SomeText&lt;/h2&gt;")

this will decode the encoded data to html and will store it in DB u r trying to decode and thn u r again encoding it thats the reason u getting the html encode data in DB

这将把编码后的数据解码成html,并将其存储在DB u r中尝试解码,然后再进行编码,这就是为什么要用DB来进行html编码

#2


1  

if you are using submit button:

如果您正在使用提交按钮:

<input type="submit" value="save html" onclick="saveIt()" />

do like this:

这样做:

function saveIt(){
     $('.editor').val(encodeURIComponent($('.editor').val()));
}

or If you just saving its value by ajax you then you can post encodeURIComponent of the html instead of html itself.

或者,如果您只是通过ajax保存它的值,那么您可以发布html的encodeURIComponent,而不是html本身。

now WebUtility.HtmlDecode should work perfectly.

现在WebUtility。HtmlDecode应该完美的工作。

#1


1  

Simply use

简单地使用

@Html.Raw("&lt;h2&gt;SomeText&lt;/h2&gt;")

this will decode the encoded data to html and will store it in DB u r trying to decode and thn u r again encoding it thats the reason u getting the html encode data in DB

这将把编码后的数据解码成html,并将其存储在DB u r中尝试解码,然后再进行编码,这就是为什么要用DB来进行html编码

#2


1  

if you are using submit button:

如果您正在使用提交按钮:

<input type="submit" value="save html" onclick="saveIt()" />

do like this:

这样做:

function saveIt(){
     $('.editor').val(encodeURIComponent($('.editor').val()));
}

or If you just saving its value by ajax you then you can post encodeURIComponent of the html instead of html itself.

或者,如果您只是通过ajax保存它的值,那么您可以发布html的encodeURIComponent,而不是html本身。

now WebUtility.HtmlDecode should work perfectly.

现在WebUtility。HtmlDecode应该完美的工作。