精进不休 .NET 4.0 (2)2012-05-28 博客园 webabcdasp.net 4.0 新特性之url路由, 自定义CacheProvider, 新增的表达式<!--expression-->, QueryExtender控件, 其它新特性介绍asp.net 4.0 的新增功能* 在 web form 中做 url 路由* 通过实现自定义的 CacheProvider ,来实现自定义的页面缓存逻辑* 新增的表达式 <%: expression %> 相当于 <%= HttpUtility.HtmlEncode(expression) %>* 控件 QueryExtender,对数据源控件获得的数据做再检索* 其它新特性示例1、web form 中的 url 路由的 demoGlobal.asax.cs代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;
namespace AspDotNet
{
     public class Global : System.Web.HttpApplication
     {
         protected void Application_Start(object sender, EventArgs e)
         {
             // 关于 Routing 可以参考以前写的  http://www.cnblogs.com/webabcd/archive/2009/04/21/1440149.html
             // 其中 PageRouteHandler 类的作用是将 URL 路由的功能集成到 Web Form 中
             RouteTable.Routes.Add("myRoute", new Route("user/{userName}/{age}", new PageRouteHandler ("~/UrlRouting/Default.aspx")));
             /* 对应的配置如下,在 machine.config 中
             <system.web>
                <httpmodule>
                     <add name="RoutingModule" type="System.Web.Routing.UrlRoutingModule"/>
                </httpmodule>
             <system.web>
              */
         }
     }
}UrlRouting/Default.aspx代码<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
     CodeBehind="Default.aspx.cs" Inherits="AspDotNet.UrlRouting.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
     <!--
         在页面上获取 url 路由而来的数据的方法
         配合以下逻辑的路由规则是:"user/{userName}/{age}"
     -->
     <asp:Literal runat="server" Text="<%$ RouteValue:userName%>" />
     <br />
     <asp:Literal runat="server" Text="<%$ RouteValue:age%>" />
     <!--
         另外,对于数据源控件来说,也多了一种参数类型 RouteParameter 
     -->
</asp:Content>
<%--
对应的配置如下,在 machine.config 中
<system.web>
    <compilation debug="true" targetFrameworkMoniker=".NETFramework,Version=v4.0">
       <expressionBuilders>
          <add expressionPrefix="RouteValue" type="System.Web.Compilation.RouteValueExpressionBuilder" />
       </expressionBuilders>
    </compilation>
<system.web>
--%>