asp.net 4.0 新特性(翻译)
原文地址:http://geekswithblogs.net/ranganh/archive/2009/08/14/whatrsquos-new-in-asp.net-4.0.aspx
Visual Studio 2010 Beta 1 和.NET Framework Beta 1已经发布一些时间啦,我使用也有一段时间,在 ASP.NET 4.0中将开发模板改进为多场景如Webform,动态数据,基于AJAX的Web开发程序。还有一些对核心对象的改进
如Asp.net的Caching,Session,Request,Response.
我体验一下WebFrom的增强,在个过去有很多体验。
(1)控制ViewState使用ViewStateMode的属性,使得性能得到改进
asp.net WebForm的ViewState性能一直都是被受争议。在过去我们所有的控件的默认都是inherit,而虽然我们可以通过设置EaableViewState属性来控制,但他们的对控件产生的行为是不一致的。
在ASP.NET 4.0中,每一个控件的ViewState的属性都是由于ViewStateMode属性控制。 分别有三个属性:enabled,disabled,inherited.
<asp:Panel ID="pnlViewState" runat="server" ViewStateMode="Disabled">
Disabled: <asp:Label ID="label1" runat="server" Text="Value set in markup" ViewStateMode="Inherit" /><br />
Enabled: <asp:Label ID="label2" runat="server" Text="Value set in markup" ViewStateMode="Enabled" />
<hr />
<asp:button ID="Button1" runat="server" Text="Postback" />
</asp:Panel>
Disabled: <asp:Label ID="label1" runat="server" Text="Value set in markup" ViewStateMode="Inherit" /><br />
Enabled: <asp:Label ID="label2" runat="server" Text="Value set in markup" ViewStateMode="Enabled" />
<hr />
<asp:button ID="Button1" runat="server" Text="Postback" />
</asp:Panel>
后台代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
label1.Text = "Value set in code behind";
label2.Text = "Value set in code behind";
}
}
{
if (!IsPostBack)
{
label1.Text = "Value set in code behind";
label2.Text = "Value set in code behind";
}
}
你就能找到在初始值,当你点击button时,你会发现label1的值改变,但Label2的值没有变化,就如你看到的,Panel中包含两个控件的ViewStateMode的设置分别为为Disabled,Enabled,但他们并没有同时受到Panel中的ViewStateMode设置为Disabled
的影响,而是由他们分别的设置来决定。
虽然使用EnableViewState的使用很容易,但考虑到性能方面的情况下在不授权(disabling)ViewState的情况下和然后又在每一个控件中授权是用ViewState,控件是不工作的,就在这个时候,ViewStateMode属性就可被使用上。
(2) Page Meta keyword & Description -Sreach Engine Optimization 特性。
在Vs2008中设Title,是通过Page.Title.而在Web应用程序中,搜索引擎是搜索的Title, Keyword 和 description ,尽管关键字在现在的搜索引擎已经基本忽略,但 Description
仍然是很主要的要素,特别Google, Bing来说于识别和索引页面的内容都是基于Description中的Content。
所在asp.net 4.0中可以使用编程的方式来设置Description 和 Keywords。
protected void Page_Load(object sender, EventArgs e)
{
this.Page.Title = "My ASP.NET Blog";
this.Page.MetaKeywords = "ASP.NET, Web Development, Blog, ASP.NET Blog";
this.Page.MetaDescription = "This Blog contains posts related to ASP.NET and Web Development";
}
{
this.Page.Title = "My ASP.NET Blog";
this.Page.MetaKeywords = "ASP.NET, Web Development, Blog, ASP.NET Blog";
this.Page.MetaDescription = "This Blog contains posts related to ASP.NET and Web Development";
}
<meta name="keywords" content="ASP.NET, Web Development, Blog, ASP.NET Blog" />
<meta name="description" content="This Blog contains posts related to ASP.NET and Web Development" />
这样做,程序会将代码中的字符放meta标签中的Content中。
尽管很简单,但非常的有用,能动态的设置页面上的一组按条件/指标的标记,所以现在Page类能动态的设置这些属性。
还有一个很重要的增强就是Routing 的改进。能设置ClientID等。
worksguo
www.cnblogs.com/worksguo