【问题标题】:How to return pure html with Mvc JsonResult如何使用 Mvc JsonResult 返回纯 html
【发布时间】:2013-10-09 03:59:23
【问题描述】:

在 Asp.net MVC 中,我想以 JSON 格式返回 HTML,以便我可以在 android 应用程序中访问此 html。

public ActionResult GetContactText()
        {
            string str = "";
            CM cms = objcms.GetCMSData();
            if (cms != null)
            {
                str = cms.ContactUs;
            }
            return Json(str, JsonRequestBehavior.AllowGet);
        }

Link

在我的数据库中,值为"<p>Hare Krsna !</p>",但是当我在浏览器中看到或从其他平台调用它时,它会返回

"\u003cp\u003eHare Krsna !\u003c/p\u003e\r\n"

这个结果。如何使用 Asp.net MVC 3 返回纯 html。

请帮忙!

【问题讨论】:

  • 用JsonResult代替ActionResult怎么样?

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


【解决方案1】:

您是否在 str 变量返回之前检查过它是否在 Json() 方法之前或之中进行了 html 编码?

您可以使用它,因为您不想返回 JSON 对象:

return Content(str, "text/html");

Json 对象必须以“{”或“[”开头,表示对象或数组。

如果您需要将返回格式设置为 JSON,则需要将 html 放入字符串中:

return Json(new { html = str }, JsonRequestBehavior.AllowGet);

返回的 JSON 将如下所示:

{
    "html": "<div> content <div>"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多