MVC ASPX(webForm)視圖引擎 <%:%> 與<%=%>的差別

來源:互聯網
上載者:User

標籤:blog   io   ar   for   sp   div   2014   art   on   

控制器

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcApplication2.Controllers{    public class HomeController : Controller    {        //        // GET: /Home/        public ActionResult Index()        {            ViewData["Script"] = "<script>alert('Dome')</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>Index</title></head><body>    <div>        <!-- 假設用=號的話,就是原封不動的輸出ViewData["Script"]中的代碼,這樣easy遭到跨站指令碼的攻擊,不夠安全,所以這裡是彈出一個對話方塊嗎,對話方塊內容為Dome-->        <%=ViewData["Script"] %>           <!--<%: %>相當於<%=Html.Encode(<script>alert('Dome')</script>) %> 推薦大家用冒號,這裡輸出的是<script>alert('Dome')</script>-->          <%:ViewData["Script"] %>         <!--假如說我如今就是想在前台頁面輸出一些Html標籤,或一些代碼,就想讓它原封不動的去運行那段代碼,比方說我如今就想給一個Div標籤放到頁面去,怎麼辦呢,下面三種方法都能夠,並且不用=號更安全-->        <%:Html.Raw("<div>我是div標籤</div>") %>        <%:new HtmlString("<p>我是p標籤</p>") %>        <%:new MvcHtmlString("<p>我也是p標籤</p>") %>           </div></body></html>



MVC ASPX(webForm)視圖引擎 &lt;%:%&gt; 與&lt;%=%&gt;的差別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.