ASP.Net提交HTML代码(编码 - 解码)

时间:2014-07-23 13:53:58

标签: html asp.net asp.net-mvc

我有一个asp.net mvc应用程序,我想在帖子上提交一个HTML代码,我有一个自定义绑定器,我有这个html代码,我在编辑器中提交

<div id="content-home-rect">
    <div class="rect-home"></div>
    <div class="rect-home"></div>
    <div class="rect-home"></div>
    <div class="rect-home"></div>
</div>

当我提交时我得到了这个

%3Cdiv%3E%26lt%3Bdiv%20id%3D%22content-home-rect%22%26gt%3B%3C%2Fdiv%3E%3Cdiv%3E%26nbsp%3B%20%26nbsp%3B%20%26lt%3Bdiv%20class%3D%22rect-home%22%26gt%3B%26lt%3B%2Fdiv%26gt%3B%3C%2Fdiv%3E%3Cdiv%3E%26nbsp%3B%20%26nbsp%3B%20%26lt%3Bdiv%20class%3D%22rect-home%22%26gt%3B%26lt%3B%2Fdiv%26gt%3B%3C%2Fdiv%3E%3Cdiv%3E%26nbsp%3B%20%26nbsp%3B%20%26lt%3Bdiv%20class%3D%22rect-home%22%26gt%3B%26lt%3B%2Fdiv%26gt%3B%3C%2Fdiv%3E%3Cdiv%3E%26nbsp%3B%20%26nbsp%3B%20%26lt%3Bdiv%20class%3D%22rect-home%22%26gt%3B%26lt%3B%2Fdiv%26gt%3B%3C%2Fdiv%3E%3Cdiv%3E%26lt%3B%2Fdiv%26gt%3B%3C%2Fdiv%3E

这是我的自定义活页夹

var req = controllerContext.HttpContext.Request.Unvalidated.Form;
var model = new ContenutiDetailModel();

foreach (var item in lingue)
{
  string html = req.Get("text-" + item);


   html = System.Web.HttpContext.Current.Server.HtmlDecode(html);


    var ol = new ObjectLingue();
       ol.content = html;
       ol.lingua = item;
       ol.id = req.Get("id-" + item);
       model.html.Add(ol);
}

return model;

html解码让我回到相同的字符串,我不知道为什么。

1 个答案:

答案 0 :(得分:1)

您提供的字符串似乎是HTML编码的网址编码。您需要反转两种编码操作才能恢复原始字符串。

它似乎还插入了额外的<div>标签。您需要查看值的发布方式以找出原因。

NB:您不需要通过System.Web.HttpContext.Current.Server来访问HtmlDecode功能;它可以在静态HttpUtility类上使用。

string html = req.Get("text-" + item);
// %3Cdiv%3E%26lt%3Bdiv%20id%3D%22content-home-rect%22%26gt...

html = HttpUtility.UrlDecode(html);
// <div>&lt;div id="content-home-rect"&gt;</div>...

html = HttpUtility.HtmlDecode(html);
// <div><div id="content-home-rect"></div>...