“=”号和“:”的区别,Html.Raw()的使用

时间:2023-03-08 19:41:42
“=”号和“:”的区别,Html.Raw()的使用

“=”号,将原封不动输出字符串到页面

“:”号:将字符串进行编码后输出到页面

  public ActionResult HtmlEncodeDemo()
         {
             ViewData["strScript"] = "<script>alert('demo');</script>";
             return View();
         }

后台代码

 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

 <!DOCTYPE html>

 <html>
 <head runat="server">
     <meta name="viewport" content="width=device-width" />
     <title>HtmlEncodeDemo</title>
 </head>
 <body>
     <div>
         <%=ViewData["strScript"] %><%--原封不动的输出字符串--%>
         <%:ViewData["strScript"] %><%--编码后输出字符串--%>
         <%:Html.Raw("<p>我是HtmlRow</p>") %>如果使用冒号,用HtmlRow也可以原封不动的输出字符串
     </div>
 </body>
 </html>

前台代码