標籤: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)視圖引擎 <%:%> 與<%=%>的差別