【问题标题】:how to escape HTML or unescape HTML?如何转义 HTML 或取消转义 HTML?
【发布时间】:2021-07-30 12:50:12
【问题描述】:

我有一个使用 jquerybuilder 返回结果的查询,但结果为空,因为它需要将"<" 转换为 html 结果中的"<";。当我从搜索栏中启动小于“某物”时,它显示空结果

这是我要添加此更改的代码:


// GET: Search/GetJsonForQuery
public JsonResult GetJsonForQuery(ObjectJson serializedJson) {

    if (serializedJson.MainObjects == null) {
        JsonResult result = Json(new {
            updated = DateTime.UtcNow.ToString("o"),
            errorNoItemSelected = true
        },
        JsonRequestBehavior.AllowGet);

        result.MaxJsonLength = Int32.MaxValue;
        return result;
    }

    if (!IsColumnSelected(serializedJson)) {
        JsonResult result = Json(new {
            updated = DateTime.UtcNow.ToString("o"),
            errorNoColumnSelected = true
        },
        JsonRequestBehavior.AllowGet);

        result.MaxJsonLength = Int32.MaxValue;
        return result;
    }
}

【问题讨论】:

  • 你能改写你的问题吗?您是在问如何转义 HTML 或取消转义 HTML?
  • 这能回答你的问题吗? Escape text for HTML

标签: c# html json query-builder


【解决方案1】:

不太确定确切的问题?有多种方法可以转义/取消转义 HTML。下面建议的答案只需要System.Net

using System.Net;
void Main()
{
   string encoded = WebUtility.HtmlEncode("<p>hello</p>");
   
   //&lt;p&gt;take me to the river&lt;/p&gt;
   Console.WriteLine($"encoded: {encoded}");

   string decoded = WebUtility.HtmlDecode("&lt;p&gt;hello&lt;/p&gt;");
   //<p>hello</p>
   Console.WriteLine($"decoded: {decoded}");

}
public class FormatObject {

        public int Id { get; set; }

        public int IdObject { get; set; }

        public string Conditions { get; set; }

        public List<int> SelectedAttributesId { get; set; }

        public override string ToString() {
            string result = "[" + Id + "," + IdObject + ", \"" + Conditions + "\"";
            if (SelectedAttributesId != null)
                result += ", [" + string.Join(",", SelectedAttributesId.ToArray()) + "]";

            return result + "]";
        }

    }
}

【讨论】:

  • 这在我的情况下不起作用我有这个 JsonResult 结果 = Json(new { updated = DateTime.UtcNow.ToString("o"), errorNoItemSelected = true,input = WebUtility.HtmlEncode(" <") }, JsonRequestBehavior.AllowGet);
  • @zine31 呃...好吧,因为它已经被编码了,在这种情况下,听起来你想让WebUtility.HtmlDecode("&amp;lt;") 把它恢复到"&lt;"
  • 总是不返回结果
  • 我不认为它是一个好用的类,因为我使用的是 jsonResult 而不是字符串;一些想法?
  • 您能否通过原始帖子中的示例说明问题所在?编码/解码问题不是很清楚。您正在创建一个新的 Json 结果,因此可以解决问题。
猜你喜欢
  • 1970-01-01
  • 2010-10-14
  • 2019-03-02
  • 2015-05-16
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多