Lập trình .net 4.0 và visual studio 2010 part 37 ppsx

6 303 0
Lập trình .net 4.0 và visual studio 2010 part 37 ppsx

Đang tải... (xem toàn văn)

Thông tin tài liệu

CHAPTER 10  ASP.NET 244 Further Control Enhancements There are a number of control enhancements, and a couple of miscellaneous new controls as well. Wizard Control The Wizard control now contains new templating functionality (LayoutTemplate). ListView Enhancements In previous versions of ASP.NET, when a row was selected within a ListView (or GridView) and the user moved to another page, the selection was maintained on the next page. This can be bad news if you then use this selection to perform an action on the selected record. ASP.NET 4.0 resolves this problem with the new EnablePersistedSelection property. If EnablePersistedSelection is set to True, then row selection is maintained using the datakey of each item. Another welcome change is that the declaration of ListViews has been much simplified. The following code shows how a ListView control had to be declared previously: <asp:ListView ID="lstView" runat="server"> <LayoutTemplate> <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder> </LayoutTemplate> <ItemTemplate> <%# Eval("firstname")%> </ItemTemplate> </asp:ListView> ASP.NET 4.0 allows you to do the following: <asp:ListView ID="ListView1" runat="server"> <ItemTemplate> <%# Eval("firstname") %> </ItemTemplate> </asp:ListView> GridView The GridView control now supports persisted selection (see previous example) and offers the ability to style header columns when they are sorted and contains improved support for working with ViewState disabled. CompareValidator The CompareValidator now supports the comparison of Time and DateTime values. CHAPTER 10  ASP.NET 245 Query Extender Query extender is a new control that aims to make the filtering of data easier by providing a declarative query syntax that you can link to the Entity or LinqDataSource controls. Browser capability files Browser capability files are to determine how best to render content for individual browsers and are held in XML format. If you feel the need (perhaps to override the rendering capabilities for iPhones, for example) you can create your own browser provider by deriving from the HttpCapabilitiesProvider class. Auto-Start Web Applications Some ASP.NET applications perform a lot of startup work (usually in Global.asax’s Application_Load() method). For example, preloading or caching data. ASP.NET 4.0 introduces a new feature called auto- start that enables you to define code to be run as soon as your application is installed (or the app pool is recycled). Until this startup work has completed ASP.NET will prevent access to the application. Not all applications will be able to benefit from this facility through as they must • Be running on Windows Server 2008 R2 and IIS 7.5. • Be written in ASP.NET 4.0. • To use add the setting below to the applicationHost.config file (held at C:\Windows\System32\inetsrv\config\): <applicationPools> <add name="ApressAppPool" startMode="AlwaysRunning"" /> </applicationPools> You must now specify the sites to be automatically started and the serviceAutoStartProvider they should use: <sites> <site name="ApressSite" id="5"> <application path="/" serviceAutoStartEnabled ="true" serviceAutoStartProvider ="PrewarmMyCache" > </application> </site> </sites> An auto start class is created by implementing the IProcessHostPreloadClient interface and added as a provider in applicationHost.config: <serviceAutoStartProviders > <add name="StartmeUp" type="Apress.CustomInitialization, ASPStartup" /> </serviceAutoStartProviders > CHAPTER 10  ASP.NET 246 Compress Session State It is generally a good rule to avoid storing anything in session unless absolutely necessary but if you must ASP.NET 4.0 allows you to compress session state. Session state compression cannot be used by an in- process session so is only applicable if your application is using state or SQL Server. To compress session simply set the compressionEnabled property to true in Web.config: <sessionState compressionEnabled="true"></sessionState> Session state is compressed using the GZip algorithm. It is important to note that compressing session requires a server to do more work so could adversely impact on the performance of your application. Caching ASP.NET 4.0 gives you the option to create and utilize custom cache providers. The cache provider can be set at an application, control, and even individual request level (by overriding the GetOutputCacheProviderName() method) offering very fine grained control. To create your own cache provider you must inherit from System.Web.Caching.OutputCacheProvider. Velocity Before you create your own caching system (you crazy fool), you would be wise to take a look into Microsoft’s new free distributed caching system, Velocity. Velocity provides a huge amount of functionality and is easily utilized by both web and Windows applications. Velocity presents a view of the cache that can be spread out amongst many machines and accessed by any type of application. For more information please refer to: http://msdn.microsoft.com/en-au/library/cc645013.aspx. System.Runtime.Caching In previous versions of .NET, caching functionality was contained in the System.Web assembly. To enable easier integration for non-web clients, Microsoft has created the new System.Runtime.Caching assembly. System.Runtime.Caching contains abstract classes for creating your own cache provider, and a new class called MemoryCache. MemoryCache can be used by non-web clients and offers simple in memory caching functionality. Microsoft say that the internal implementation of MemoryCache is very similar to ASP.NET’s cache. The following example shows how to utilize MemoryCache to store a string for an hour (note you can also create watchers to invalidate the cache if an item changes and add them to the policy.ChangeMonitors property): ObjectCache cache = MemoryCache.Default; string testData = cache["someData"] as string; if (testData == null) { CacheItemPolicy policy = new CacheItemPolicy(); policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddHours(1)); cache.Set("someData", "some test data", policy); } CHAPTER 10  ASP.NET 247 Resource Monitoring Some web servers run many applications within one app pool. If issues occur on an individual site, it can be difficult for an administrator to determine which particular application is having difficulties. ASP.NET 4.0 introduces additional performance counters allowing you to monitor individual applications CPU and memory usage. To utilize this, you must add the appDomainResourceMonitoring setting to Aspnet.config (Aspnet.config is located where the .NET framework is installed: C:\Windows\Microsoft.NET\ Framework\v4.0.21006): <runtime> <appDomainResourceMonitoring enabled="true"/> </runtime> When you have added this line go into perfmon and you should find that you will have access to two new performance counters in the ASP.NET applications performance category section (Figure 10.6): • Managed Processor Time • Managed Memory Used Figure 10-6. New perf counters for ASP.NET CHAPTER 10  ASP.NET 248 Charting Controls Microsoft purchased and integrated the Dundas ASP.NET charting controls in early 2008. This set contains over 35 different types of charts and a huge amount of functionality, some of which is shown in Figure 10-7. Previously these controls had to be installed as an add-on and a number of settings added to Web.config. ASP.NET 4.0, however, includes these controls, and it is no longer necessary to make changes to Web.config to include them. Figure 10-7. A simple ASP.NET chart To add a chart to your web page simply drag the Chart control from the toolbox or add a reference to System.Web.DataVisualization and the Register directive below (note this may be slightly different for the final release of VS2010): <%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %> Charts are then added to the page with the following code: <asp:Chart runat="server"> <series><asp:Series Name="Series1"></asp:Series></series> <chartareas><asp:ChartArea Name="ChartArea1"></asp:ChartArea></chartareas> </asp:Chart> CHAPTER 10  ASP.NET 249 The following code binds a series of random points to the chart: Random r = new Random(); Series series = new Series("Line"); series.ChartType = SeriesChartType.Line; for (int i = 0; i < 100; i++) { series.Points.AddY(r.Next(0,100)); } chart1.Series.Add(series); Dynamic Data Framework It is worth noting that the Dynamic Data Framework has a number of additions in VS2010/ASP.NET 4.0. I will not be covering these but for interested readers please refer to: http://www.asp.net/dynamicdata. Conclusion ASP.NET 4.0 fixes some long-term omissions and bugs and looks likely to continue to be a popular method of developing web applications. Alternative approaches such as ASP.NET MVC (Chapter 13) are gaining ground (due in part to their easy testability), so it will be interesting to see if this remains the case in years to come. Further Reading • http://www.asp.net/LEARN/whitepapers/aspnet4/default.aspx . number of additions in VS 201 0/ASP .NET 4. 0. I will not be covering these but for interested readers please refer to: http://www.asp .net/ dynamicdata. Conclusion ASP .NET 4. 0 fixes some long-term. appDomainResourceMonitoring setting to Aspnet.config (Aspnet.config is located where the .NET framework is installed: C:WindowsMicrosoft .NET Frameworkv4 .0. 2 100 6): <runtime> <appDomainResourceMonitoring. the ASP .NET applications performance category section (Figure 10. 6): • Managed Processor Time • Managed Memory Used Figure 10- 6. New perf counters for ASP .NET CHAPTER 10  ASP .NET 248 Charting

Ngày đăng: 01/07/2014, 21:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan