DOC

ASP.NET4.0_and_Visual_Studio_2010_Web_Development_Beta_1_Overview

By Roger Washington,2014-06-02 22:13
7 views 0
ASP.NET4.0_and_Visual_Studio_2010_Web_Development_and_Visual_Studio_2010_Web_Development

ASP.NET 4.0 and Visual Studio 2010 Web

    Development Beta 1 Overview

    Many exciting changes for ASP.NET are coming in the .NET Framework version 4.0. This document gives

    an overview of many of the new features that are included in the upcoming Beta 1 release of .NET 4.0

    and in the Visual Studio 2010 release.

    Contents

    Core Services .......................................................................................................................................... 2 Extensible Output Caching ................................................................................................................... 2 Auto-Start Web Applications................................................................................................................ 4 Permanently Redirecting a Page .......................................................................................................... 5 The Incredible Shrinking Session State ................................................................................................. 6 AJAX Functionality in ASP.NET 4.0 .......................................................................................................... 6 Client Template Rendering .................................................................................................................. 7 Instantiating Behaviors and Controls Declaratively............................................................................... 9 Live Data Binding ............................................................................................................................... 11 Using the Observer Pattern with JavaScript Objects and Arrays ......................................................... 13

    The DataView Control ........................................................................................................................ 14 The AdoNetServiceProxy Class ........................................................................................................... 16 The DataContext and AdoNetDataContext Classes ............................................................................. 16

    Refactoring the Microsoft AJAX Framework Libraries ......................................................................... 17 Web Forms ........................................................................................................................................... 20 Setting Meta Tags with the Page.Keywords and Page.Description Properties ..................................... 20

    Enabling View State for Individual Controls ........................................................................................ 21 Changes to Browser Capabilities ........................................................................................................ 23 Routing in ASP.NET 4.0 ...................................................................................................................... 27 Setting Client IDs ............................................................................................................................... 31 Persisting Row Selection in Data Controls .......................................................................................... 34 FormView Control Enhancements ...................................................................................................... 35 ListView Control Enhancements ......................................................................................................... 36 Filtering Data with the QueryExtender Control .................................................................................. 36 ASP.NET 4.0 and Visual Studio 2010 Web Development Overview 1

    ? 2009 Microsoft Corporation

    Dynamic Data ....................................................................................................................................... 38 Declarative DynamicDataManager Control Syntax ............................................................................. 39

    Entity Templates ................................................................................................................................ 39 New Field Templates for URLs and E-mail Addresses.......................................................................... 41

    Creating Links with the DynamicHyperLink Control ............................................................................ 41 Support for Inheritance in the Data Model......................................................................................... 42 Support for Many-to-Many Relationships (Entity Framework Only).................................................... 42

    New Attributes to Control Display and Support Enumerations ........................................................... 42

    Enhanced Support for Filters.............................................................................................................. 42 Visual Studio 2010 Web Designer Improvements ................................................................................. 43 Improved CSS Compatibility ............................................................................................................... 43 HTML and JScript Snippets ................................................................................................................. 43 JScript IntelliSense Enhancements ..................................................................................................... 43 Web Application Deployment with Visual Studio 2010......................................................................... 43 Web Packaging .................................................................................................................................. 44 Web.Config Transformation ............................................................................................................... 45 Database Deployment ....................................................................................................................... 45 One-Click Publishing .......................................................................................................................... 45 Resources .......................................................................................................................................... 45 Disclaimer ............................................................................................................................................. 47

Core Services

    ASP.NET 4 introduces a number of features that improve core ASP.NET services such as output caching

    and session-state storage.

    Extensible Output Caching

    Since ASP.NET 1.0 was released, output caching has enabled developers to store the generated output

    of pages, controls, and HTTP responses in memory. On subsequent Web requests, ASP.NET can serve

    content more quickly by retrieving the generated output from memory instead of regenerating the

    output from scratch. However, this approach has a limitation generated content always has to be

    stored in memory, and on heavily trafficked servers, the memory consumed by output caching can

    compete with memory demands from other portions of a Web application.

    ASP.NET 4.0 and Visual Studio 2010 Web Development Overview 2

    ? 2009 Microsoft Corporation

ASP.NET 4.0 adds an extensibility point to output caching that enables you to configure one or more

    custom output-cache providers. Output-cache providers can use any storage mechanism to persist

    HTML content. This makes it possible to create custom output-cache providers for diverse persistence

    mechanisms, which can include local or remote disks, cloud storage, and distributed cache engines.

    You create a custom output-cache provider as a class that derives from the new

    System.Web.Caching.OutputCacheProvider type. You can then configure the provider in the Web.config

    file by using the new providers subsection of the outputCache element, as shown in the following example:

    <caching>

     <outputCache defaultProvider="AspNetInternalProvider">

     <providers>

     <add name="DiskCache"

     type="Test.OutputCacheEx.DiskOutputCacheProvider, DiskCacheProvider"/>

     providers>

     outputCache>

    caching>

    By default in ASP.NET 4.0, all HTTP responses, rendered pages, and controls use the in-memory output

    cache, as shown in the previous example, where the defaultProvider attribute is set to AspNetInternalProvider. You can change the default output-cache provider used for a Web

    application by specifying a different provider name for defaultProvider. In addition, you can select different output-cache providers per control and per request. The easiest way

    to choose a different output-cache provider for different Web user controls is to do so declaratively by

    using the new providerName attribute in a page or control directive, as shown in the following example:

    <%@ OutputCache Duration="60" VaryByParam="None" providerName="DiskCache" %> Specifying a different output cache provider for an HTTP request requires a little more work. Instead of

    declaratively specifying the provider, you instead override the new GetOuputCacheProviderName

    method in the Global.asax file to programmatically specify which provider to use for a specific request.

    The following example shows how to do this.

    public override string GetOutputCacheProviderName(HttpContext context) {

     if (context.Request.Path.EndsWith("Advanced.aspx"))

     return "DiskCache";

     else

     return base.GetOutputCacheProviderName(context);

    }

    With the addition of output-cache provider extensibility to ASP.NET 4.0, you can now pursue more

    aggressive and more intelligent output-caching strategies for your Web sites. For example, it is now

    ASP.NET 4.0 and Visual Studio 2010 Web Development Overview 3

    ? 2009 Microsoft Corporation

possible to cache the "Top 10" pages of a site in memory, while caching pages that get lower traffic on

    disk. Alternatively, you can cache every vary-by combination for a rendered page, but use a distributed

    cache so that the memory consumption is offloaded from front-end Web servers.

     Auto-Start Web Applications

    Some Web applications need to load large amounts of data or perform expensive initialization

    processing before serving the first request. In earlier versions of ASP.NET, for these situations you had to

    devise custom approaches to "wake up" an ASP.NET application and then run initialization code during

    the Application_Load method in the Global.asax file. A new scalability feature named auto-start that directly addresses this scenario is available when ASP.NET 4.0 runs on IIS 7.5 on Windows Server 2008 R2. The auto-start feature provides a controlled

    approach for starting up an application pool, initializing an ASP.NET application, and then accepting

    HTTP requests.

    To use the auto-start feature, an IIS administrator sets an application pool in IIS 7.5 to be automatically

    started by using the following configuration in the applicationHost.config file:

    <applicationPools>

     <add name="MyApplicationPool" startMode="AlwaysRunning" />

    applicationPools>

    Because a single application pool can contain multiple applications, you specify individual applications to

    be automatically started by using the following configuration in the applicationHost.config file:

    <sites>

     <site name="MySite" id="1">

     <application path="/"

     preloadEnabled="true"

     preloadProvider="PrewarmMyCache" >

    

     application>

     site>

    sites>

<preloadProviders>

     <add name="PrewarmMyCache"

     type="MyNamespace.CustomInitialization, MyLibrary" />

    preloadProviders>

    ASP.NET 4.0 and Visual Studio 2010 Web Development Overview 4

    ? 2009 Microsoft Corporation

When an IIS 7.5 server is cold-started or when an individual application pool is recycled, IIS 7.5 uses the

    information in the applicationHost.config file to determine which Web applications need to be automatically started. For each application that is marked for auto-start, IIS7.5 sends a request to

    ASP.NET 4.0 to start the application in a state during which the application temporarily does not accept

    HTTP requests. When it is in this state, ASP.NET instantiates the type defined by the preloadProvider

    attribute (as shown in the previous example) and calls into its public entry point.

    You create a managed auto-start type with the necessary entry point by implementing the

    IProcessHostPreloadClient interface, as shown in the following example:

    public class CustomInitialization : System.Web.Hosting.IProcessHostPreloadClient {

     public void Preload(string[] parameters)

     {

     // Perform initialization.

     }

    }

    After your initialization code runs in the Preload method and the method returns, the ASP.NET

    application is ready to process requests.

    With the addition of auto-start to IIS 7.5 and ASP.NET 4.0, you now have a well-defined approach for

    performing expensive application initialization prior to processing the first HTTP request. For example,

    you can use the new auto-start feature to initialize an application and then signal a load-balancer that

    the application was initialized and ready to accept HTTP traffic.

    Permanently Redirecting a Page

    It is common practice in Web applications to move pages and other content around over time, which

    can lead to an accumulation of stale links in search engines. In ASP.NET, developers have traditionally

    handled requests to old URLs by using by using the Response.Redirect method to forward a request to

    the new URL. However, the Redirect method issues an HTTP 302 Found (temporary redirect) response, which results in an extra HTTP round trip when users attempt to access the old URLs.

    ASP.NET 4.0 adds a new RedirectPermanent helper method that makes it easy to issue HTTP 301 Moved Permanently responses, as in the following example:

    RedirectPermanent("/newpath/foroldcontent.aspx");

    Search engines and other user agents that recognize permanent redirects will store the new URL that is

    associated with the content, which eliminates the unnecessary round trip made by the browser for

    temporary redirects.

    ASP.NET 4.0 and Visual Studio 2010 Web Development Overview 5

    ? 2009 Microsoft Corporation

The Incredible Shrinking Session State

    ASP.NET provides two default options for storing session state across a Web farm: a session-state

    provider that invokes an out-of-process session-state server, and a session-state provider that stores

    data in a Microsoft SQL Server database. Because both options involve storing state information outside

    a Web application's worker process, session state has to be serialized before it is sent to remote storage.

    Depending on how much information a developer saves in session state, the size of the serialized data

    can grow quite large.

    ASP.NET 4.0 introduces a new compression option for both kinds of out-of-process session-state

    providers. When the compressionEnabled configuration option shown in the following example is set to

    true, ASP.NET will compress (and decompress) serialized session state by using the .NET Framework

    System.IO.Compression.GZipStream class. <sessionState

     mode="SqlServer"

     sqlConnectionString="data source=dbserver;Initial Catalog=aspnetstate"

     allowCustomSqlDatabase="true"

     compressionEnabled="true" />

    With the simple addition of the new attribute to the Web.config file, applications with spare CPU cycles

    on Web servers can realize substantial reductions in the size of serialized session-state data.

    AJAX Functionality in ASP.NET 4.0

    The AJAX functionality in ASP.NET 4.0 enables new client data scenarios for page and component

    developers that allow JSON data from the server to be rendered as HTML in a highly manageable and

    efficient way. To enable these scenarios, ASP.NET 4.0 includes the following major features:

    ? Client-side template rendering.

    ? Declarative instantiation of client-side behaviors and controls.

    ? Live data binding.

    ? Use of the observer pattern with JavaScript types.

    ? An AdoNetServiceProxy class for client-side interaction with ADO.NET Data Services. ? A client-side DataView control for data-bound UI in the browser. ? DataContext and AdoNetDataContext classes for interaction with Web services.

    ? Refactored ASP.NET AJAX framework libraries.

    ASP.NET 4.0 and Visual Studio 2010 Web Development Overview 6

    ? 2009 Microsoft Corporation

Client Template Rendering

    In client-based development, templates are the most manageable way of creating UI from data. ASP.NET

    4.0 includes a new template engine for client development that meets the following requirements:

    ? Performance The engine must be able to render a typical number of items using a reasonably

    complex template before users perceive an interruption in their interaction with the application.

    ? Simplicity The template syntax must be very readable and must be optimized for the most

    common scenario, namely one-way/one-time binding.

    ? Expression language Templates must support an expression language to go beyond the simplest

    cases. The expression language should use familiar syntax, ideally JavaScript syntax.

    ? Interspersed code and markup It must be possible to perform conditional rendering or to loop

    over markup by using code that surrounds HTML.

    ? XHTML compliance The template should be able to render XHTML-compliant markup. ? Components When using the template syntax, the developer must be able to instantiate client-

    side controls and behaviors that attach to HTML elements in the page or within templates.

    Template Example

    The following example shows a typical client template that you can create using ASP.NET 4.0.

    <ul id="myTemplate" class="sys-template"

     sys:attach="dataview"

    >

     <li>

     <h3>{{ Name }}h3>

     <div>{{ Description }}div>

     li>

    ul>

    The class attribute of the outer div element is set to sys-template, which is a convention that is used in

    order to hide the initial template from the user. You should define this class in your CSS style sheet as

    {display:none;}.

    When the template is rendered, it has a data item as context. Fields or properties of that data item can

    be included in the template markup by using {{ }} expressions for example, {{ Name }}, if the data

    item has a Name field. These expressions can be placed anywhere in text content, or you can use them as

    the value of an attribute. In addition to fields or properties, the expression blocks can also contain any

    JavaScript expression that can be evaluated as a string.

    You can set up DOM events by using the $addHandler method. The DOM on* attributes of elements (for

    example, onclick=method) also work from within templates. ASP.NET 4.0 and Visual Studio 2010 Web Development Overview 7

    ? 2009 Microsoft Corporation

Instantiating a Template by Using the DataView Control

    The most convenient way to use client templates in ASP.NET 4.0 is through the DataView control. The

    content of a DataView control is used as a template that renders the data item that is provided to the

    control. If you set the DataView control's data property to an array, the template is rendered once for each item in the array. The DataView control binds to live data, meaning that the control is automatically updated when the data changes, without the need to rebind. This provides a dynamic

    data-driven UI in the browser. The following example shows the declarative markup for a DataView

    control that binds to an array named imagesArray. <body xmlns:sys="javascript:Sys"

     xmlns:dataview="javascript:Sys.UI.DataView"

     sys:activate="*">

     <ul sys:attach="dataview" class="sys-template"

     dataview:data="{{ imagesArray }}"

     >

     <li>

     <h3>{{ Name }}h3>

     <div>{{ Description }}div>

     li>

     ul>

    body>

    Instantiating a Template by Using Code

    You can also create a compiled template from code by using the Sys.UI.Template class, as shown in the

    following example:

    var t = new Sys.UI.Template($get("myTemplate")); The constructor takes the parent element of the template as its argument. You can then render a

    compiled template into the DOM by calling its instantiateIn method and specifying an HTML container

    element and a data item as context. The following example shows how to do this.

    t.instantiateIn(

     $get("targetContainer"),

     {

     Name: "Name",

     Description: "Description"

     }

    );

    ASP.NET 4.0 and Visual Studio 2010 Web Development Overview 8

    ? 2009 Microsoft Corporation

Using Pseudo-Columns in a Template

    In addition to providing access to fields and properties of the data item, the template rendering engine

    also provides access to pre-defined "pseudo-columns" such as $index and $dataItem. These pseudo-

    columns give you access to values from the rendering engine at render time. You can use pseudo-

    columns the way you use any JavaScript variable in the template instance. The first example applies a

    different CSS class to the div element for alternating items that are rendered by the dataView control.

    The second examples passes the pseudo-column $dataItem, which represents the data for the current row, to a custom function named nameConvert for processing.

    

     {{nameConvert($dataItem)}}

    Incorporating Code into a Template by Using the code:if, code:before, and code:after Attributes

    You can add the new code:if, code:before, and code:after declarative attributes to any DOM element within a template in order to render the element conditionally (code:if) or to render arbitrary code before (code:before) or after (code:after) the element. The following example shows how to use the

    code:If attribute to render an hr element as a separator between items. The code:if attribute uses the

    value of the $index pseudo-column to ensure that the hr element is rendered only between items, and not before the first one or after the last one.

    <ul id="myTemplate" class="sys-template">

     <li>

     <hr code:if="$index!==0" />

     <h3>{{ Name }}h3>

     <div>{{ Description }}div>

     li>

    ul>

    Instantiating Behaviors and Controls Declaratively

    ASP.NET 4.0 introduces a way to declaratively instantiate client-side controls and behaviors and attach

    them to HTML elements. The declarative markup is achieved without adding any new HTML elements,

    simply by including additional namespaced attributes which are XHTML compliant.

    Declarative Instantiation Within a Template

    Suppose that you want to add a Contoso.DatePicker control to a div element. In ASP.NET 4.0, you can start by declaring a namespace prefix in the opening tag (or in a template's parent tag), similar to the way the @ Register directive works in server-based files. The following example shows a

    namespace declaration.

    ASP.NET 4.0 and Visual Studio 2010 Web Development Overview 9

    ? 2009 Microsoft Corporation

<body xmlns:sys="javascript:Sys" xmlns:datepicker="javascript:Contoso.DatePicker">

    The javascript:Sys namespace (typically mapped to the sys: prefix, as shown in the example) is used for a number of system attributes. One of those system attributes is sys:attach, which is used to specify a comma-separated list of controls or behaviors to attach to the element, as shown in the

    following example:

    <ul id="myTemplate" class="sys-template">

     <li>

     <h3>{{ Name }}h3>

     <div>{{ Description }}div>

     <div sys:attach="datepicker" datepicker:date="{{ CreatedDate }}">div>

     li>

    ul>

    The example shows how to instantiate a Contoso.DatePicker control that is attached to a div element and how to set the control's date property to the value of the CreatedDate field of the current data item.

    Declarative Instantiation Outside a Template

    Declarative instantiation of controls will only work if the declarative markup is within an element that

    has been configured, or activated, for this purpose. Templates themselves are already activated, so

    declarative markup to instantiate and attach controls works within a template. But to declaratively

    instantiate controls outside a template, you must first configure the page to ensure that sys:attach

    markup is within an activated element. You do this by including a sys:activate attribute on the opening body tag, and setting its value to a comma-separated list of element IDs for the elements that you want

    to allow declarative instantiation in. The following example activates elements whose IDs are panel1

    and panel2:

    <body xmlns:sys="javascript:Sys" sys:activate="panel1,panel2">

    This causes the ASP.NET AJAX framework to scan the children of those elements for any sys:attach

    attributes, and to instantiate the corresponding controls.

    You can also activate every element in the document. However, doing so can cause a small delay when

    the page is initialized, so the technique should be used with caution on large pages. The following

    example shows how to activate the whole document.

    <body xmlns:sys="javascript:Sys" sys:activate="*">

    A DataView control is typically attached to an element that is not already within a template. This is a

    common reason to use sys:activate, as in the following complete example:

    <body xmlns:sys="javascript:Sys"

    ASP.NET 4.0 and Visual Studio 2010 Web Development Overview 10

    ? 2009 Microsoft Corporation

Report this document

For any questions or suggestions please email
cust-service@docsford.com