Asp.Net 4.0 新特性 系列 之一 从页面标记<%%>说起

VS2010在4月份已经正式发布了,Asp.Net 4.0给我带来了一些新的东西。体验一把。

 

1. 从页面标记<%%>说起
2. Asp.Net 4.0 中可以用自定义的Provider做OutputCache 了
3. SEO增强支持MetaKeywords,和MetaDescription,RedirectPermanant
4. SEO增强之URL Routing
5. 输出更纯净的Html代码,ViewStateMode和ClientIDMode,CheckBoxList等 

在Asp.Net4.0中<%符号表达式%>有四种形式,分别为<%=”hello world”%>,<%$expression%>,<%#expression%>,<%:abc%> 我们分别来看一下

 1. <%=表达式%> 这中写法支持asp,jsp等等,他就是直接输出表达式的值,它等同于

Response.Write(表达式);


2. <%$declarationExp:expression%> 这是一种声明式表达式,他在Asp.Net 2.0时被引入,我们可以利用这个表达式,输出AppSettings中的配置,给SqlDataSource控件设定连接字符串等等,在.Net 4.0时我们可以在这个表达式中输出Url Routing中的参数值,或者根据设置得到UrlRouting后的URL
例如:
1)在Literal中显示URL Routing 当前的action值

<asp:Literal ID="l" EnableViewState="false" Text="<%$RouteValue:action %>" runat="server"></asp:Literal>

2)传入参数显示Routing后的Url

<asp:Label ID="lb" EnableViewState="false" Text="<%$RouteUrl:controller=Category ,action=edit,id=1%>" runat="server"></asp:Label>

在.Net 4.0支持的系统默认支持的表达式有:Resources, ConnectionStrings,AppSettings,RouteUrl,RouteValue这些在根配置文件web.config的system.web/compilation/expressionBuilders配置节中有配置

更多介绍可以参考:http://msdn.microsoft.com/en-us/library/d5bd1tad(v=VS.100).aspx
http://msdn.microsoft.com/en-us/library/system.web.compilation.routeurlexpressionbuilder.aspx

3. <%#expression%>表示数据绑定,必须在调用DataBind方法才能输出绑定的值,通常用在DataGrid,Repeater,ListView等模板控件中。

4. <%:expression%>它是一个新增的表达式方式,表示将expression HtmlEncode后输出,如果这个表达式的类型时IhtmlString,它将通过IhtmlString接口的ToHtmlString()方法来做UrlEncode,.Net 4.0中HtmlString类通过实现这个接口,避免重复做HtmlEncode

例如:
  1. 需要做HtmlEncode时使用表达式

<%:”<h1>I’a a test</h1>”%>

  将输出

<h1>I’a a test</h1>

  2. 不需要做HtmlEncode时使用表达式

<%: new HtmlString(”<h1>I’a a test</h1>”)%>

  将输出

<h1>I’a a test</h1>

 这个很酷的feature在Asp.Net Mvc 2.0中被广泛使用。

这些表达式都出现在aspx页面上,在编译aspx页面时编译程序会使用正则比表达式将这些表达式解析成后台代码来执行

posted @ 2010-05-15 18:28  玉开  阅读(6021)  评论(4编辑  收藏  举报