Html.Encode

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MvcApp2.Views.Home.Index" %>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
</asp:Content>

 

除非知道输出的是一段Html编码的字符串,可以直接输出字符串。因为当字符串中带有空格或者<,这种可能引起浏览器曲解,就要加上Html.Encode进行Html编码的转换,以防出现一些不必要的错误。

 

微软官方说明

posted @ 2012-07-05 15:37  陈哈哈  阅读(3965)  评论(0编辑  收藏  举报