22 February 2011

New features of ASP.NET 4

Introduction

In this article, I want to give an overview of the new features of ASP.NET that are included in the .NET framework 4 and Visual Studio 2010. I will explain these new features from the problem/solution strategy.

New features

1. Extensible Output Caching

Context: What is cache? A cache is a memory area where the output of operations is stored in order to improve the performance. When we need this output again, we look for in the cache unlike to execute the operations again.

Problem: This approach has a memory limitation. If your servers are experiencing heavy traffic, then the memory consumed by the output caching can compete with memory demands from other applications or components of your own application.

Solution: The extensible output caching enables you to configure one or more custom output-cache providers for diverse persistence mechanisms such as hard drives (locally or remotely), cloud storage and distributed cache engines as Velocity.

Implementation strategy: You can create a custom output-cache provider as a class that derives from the new System.Web.Caching.OutputCacheProvider type. For example, let's create a provider using AppFabric (Velocity) as shown in the Listing 1.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using Microsoft.ApplicationServer.Caching;

namespace _01WebAppOutputCacheDemo.OutputCacheProviders
{
    public class VelocityCacheProvider : OutputCacheProvider, IDisposable
    {
        private DataCache dataCache;
        const String OutputCacheName = "OutputCache";
        public VelocityCacheProvider()
        {
            DataCacheFactory factory = new DataCacheFactory();
            this.dataCache = factory.GetCache(OutputCacheName);
        }
        public override object Add(string key, object entry, DateTime utcExpiry)
        {
            this.dataCache.Add(key, entry, utcExpiry - DateTime.UtcNow);
            return entry;
        }
        public override object Get(string key)
        {
            return this.dataCache.Get(key);
        }
        public override void Remove(string key)
        {
            this.dataCache.Remove(key);
        }
        public override void Set(string key, object entry, DateTime utcExpiry)
        {
            throw new NotImplementedException();
        }
        public void Dispose()
        {
            this.dataCache = null;
        }
    }
}

Listing 1

You can then configure the provider in the Web.config file by using the new provider subsection of the outputCache element (see Listing 2).

<caching>
  <outputCache defaultProvider="AspNetInternalProvider">
    <providers>
      <add name="VelocityCache" type="_01WebAppOutputCacheDemo.OutputCacheProviders.VelocityCacheProvider, 01WebAppOutputCacheDemo"/>
    </providers>
  </outputCache>
</caching>

Listing 2

Moreover, 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 control directive (see Listing 3).

<%@ OutputCache Duration="60" VaryByParam="None" providerName="VelocityCache" %>

Listing 3

Instead of declaratively specifying the provider, you override the new GetOuputCacheProviderName method in the Global.asax file to programmatically specify which provider to use for a specific request (see Listing 4).

public override string GetOutputCacheProviderName(HttpContext context)
{
    if (context.Request.Path.EndsWith("CacheWebForm.aspx"))
        return "VelocityCache";
    else
        return base.GetOutputCacheProviderName(context);
}

Listing 4

2. Permanently Redirecting a Page

Context: 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.

Problem: Traditionally, this is done using the Response.Redirect method to forward a request to the new URL. However, this 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.

Solution: ASP.NET 4 adds a new RedirectPermanent method that makes it easy to issue HTTP 301 Moved Permanently responses.

Implementation strategy: See an example in the Listing 5.

Response.RedirectPermanent("newlocation/page.aspx");

Listing 5

3. Shrinking Session State

Context: It is common practice in Web applications to store objects in the session state.

Problem: Depending on how much information a developer saves in session state, the size of the serialized data can grow quite large.

Solution: ASP.NET 4 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.

Implementation strategy: See an example in the Listing 6.

<sessionState
      mode="SQLServer" sqlConnectionString="data source=dbserver;Initial Catalog=aspnetstate" allowCustomSqlDatabase="true" compressionEnabled="true"/>

Listing 6

4. Setting Meta Tags

Context: The Meta tags enable you including reference information about the Web page (metadata) such as author, keywords, content, etc.

Problem: We need to dynamically include metadata in our page, for example, from a relational data source for the search engine to give more relevance to our site. This is a Search Engine Optimization (SEO) technique.

Solution: ASP.NET 4 introduces the new properties MetaKeywords and MetaDescription to the Page class. The @Page directive contains the Keywords and Description attribute.

Implementation strategy: See an example in the Listing 7.

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="_01WebAppOutputCacheDemo._Default" Keywords="This is the default page" Description="This is the default page" %>

Listing 7

This configuration generates the following output as shown in the Listing 8.

<head id="Head1" runat="server"> 
  <title>Untitled Page</title> 
  <meta name="keywords" content="These, are, my, keywords" /> 
  <meta name="description" content="This is the description of my page" /> 
</head>

Listing 8

5. Better control of the ViewState

Problem: When we use the ViewState to store a great deal of data, this causes a signficant performance degradation.

Solution: ASP.NET 4 introduces the new property ViewStateMode in the Web controls that lets you disable view state by default and then enable it only for the controls that require it in the page. This property may have three values: Enable, Disable, and Inherit. You can also set the ViewStateMode at the page level.

Implementation strategy: See an example in the Listing 9.

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="_01WebAppOutputCacheDemo._Default" ViewStateMode="Disabled" %>

Listing 9

6. Routing in ASP.NET 4

Context: To increase the traffic to our Web site, one of the most used SEO (search engine opitimization) technique is the URL normalization in order to add semantic to the URL, for example, a Web page that displays a list of products by category is traditionally accessed by the URL http://website/products.aspx?categoryid=12, if we need the URL to be descriptive, we have to transform it as http://website/products/categoryid/12.

Problem: How can we configure our applications using Web Form pages to use URL not bind to physical files in the server?

Solution: Although routing features comes with ASP.NET 3.5 SP1, ASP.NET 4 comes with new additions to make it easier to use the routing mechanisms, including the following:
  • The PageRouteHandler class, which is a simple HTTP handler that you use when you define routes. The class passes data to the page that the request is routed to.
  • The new properties HttpRequest.RequestContext and Page.RouteData (which is a shortcut to HttpRequest.RequestContext.RouteData). These properties make it easier to access information that is passed from the route.
  • The following new expression builders, which are defined in System.Web.Compilation.RouteUrlExpressionBuilder and System.Web.Compilation.RouteValueExpressionBuilder:
    • RouteUrl, which provides a simple way to create a URL that corresponds to a route URL within an ASP.NET server control.
    • RouteValue, which provides a simple way to extract information from the RouteContext object.
  • The RouteParameter class, which makes it easier to pass data contained in a RouteContext object to a query for a data source control (similar to FormParameter).
Implementation strategy:

This is the main steps to add routing capabilities to your application.
  • Open the Global.asax file and add the following code to bind the products/category/{id} url pattern with the physical page ProductsCategory.aspx (see Listing 10).

    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
    _02WebAppRoutingDemo
    {
        public class Global : System.Web.HttpApplication
        {
            // New code added
            void RegisterRoutes(RouteCollection routes)
            {
                routes.MapPageRoute("ProductsCategory", "products/category/{id}", "~/ProductsCategory.aspx");
            }        void Application_Start(object sender, EventArgs e)
            {
                // New code added
                RegisterRoutes(RouteTable.Routes);
            }
        }
    }
    Listing 10
  • And finally, let's create the ProductsCategory.aspx file and add the code to get category value and display in the page (see Listing 11).

    using
    System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    namespace
    _02WebAppRoutingDemo
    {
        public partial class ProductsCategory : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string productCategoryId = this.Page.RouteData.Values["id"]!=null?this.Page.RouteData.Values["id"].ToString():"No Product Category";
                this.lblProductCategoryId.Text = productCategoryId;
            }
        }
    }
    Listing 11
7. Setting Client IDs

Context: Everyday, we use Ajax and JavaScript libraries to build Rich Internet Applications (RIA). The new ClientIDMode property addresses a long-standing issue in ASP.NET, namely how controls create the id attribute for elements that they render. Knowing the id attribute for rendered elements is important if your application includes client script that references these elements.

Problem: Until ASP.NET 4, the algorithm for generating the id attribute from the ClientID property has been to concatenate the naming container (if any) with the ID, and in the case of repeated controls (as in data controls), to add a prefix and a sequential number. While this has always guaranteed that the IDs of controls in the page are unique, the algorithm has resulted in control IDs that were not predictable, and were therefore difficult to reference in client script. This occurs specially when we're using master pages.

A trick to get a reference to the HTML elements is shown in the Listing 12:

var btn = document.getElementById("<% =Button1.ClientID %>");

Listing 12

Or, the more elegant solution in jQuery as shown in the Listing 13.

var btn = $('<% =Button1.ClientID %>');

Listing 13

Solution: ASP.NET 4 introduces new ClientIDMode property lets you specify more precisely how the client ID is generated for controls. You can set the ClientIDMode property for any control, including for the page. Possible settings are the following:
  • AutoID. This is equivalent to the algorithm for generating ClientID property values that was used in earlier versions of ASP.NET.
  • Static. This specifies that the ClientID value will be the same as the ID without concatenating the IDs of parent naming containers. This can be useful in Web user controls. Because a Web user control can be located on different pages and in different container controls, it can be difficult to write client script for controls that use the AutoID algorithm because you cannot predict what the ID values will be.
  • Predictable. This option is primarily for use in data controls that use repeating templates. It concatenates the ID properties of the control's naming containers, but generated ClientID values do not contain strings like "ctlxxx". This setting works in conjunction with the ClientIDRowSuffix property of the control. You set the ClientIDRowSuffix property to the name of a data field, and the value of that field is used as the suffix for the generated ClientID value. Typically you would use the primary key of a data record as the ClientIDRowSuffix value.
  • Inherit. This setting is the default behavior for controls; it specifies that a control's ID generation is the same as its parent.
Implementation strategy:

An AutoID example (see Listing 14).

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:Panel ID="PanelParent"  runat="server">         
        <asp:Panel ID="PanelChild" runat="server">         
            <asp:TextBox ID="txtEcho" runat="server" ClientIDMode="AutoID" />
        </asp:Panel>
    </asp:Panel>
</asp:Content>

Listing 14

And the output is shown in the Listing 15.

<div id="MainContent_PanelParent">          
   <div id="MainContent_PanelChild">          
     <input name="ctl00$MainContent$txtEcho" type="text" id="ctl00_MainContent_txtEcho" />  
  </div>
</div>

Listing 15

A Static example (see Listing 16).

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:Panel ID="PanelParent"  runat="server">         
        <asp:Panel ID="PanelChild" runat="server">         
            <asp:TextBox ID="txtEcho" runat="server" ClientIDMode="Static" />
        </asp:Panel>
    </asp:Panel>
</asp:Content>

Listing 16

The output is shown in the Listing 17.

<div id="MainContent_PanelParent">          
  <div id="MainContent_PanelChild">          
    <input name="ctl00$MainContent$txtEcho" type="text" id="txtEcho" />   
   </div> 
</div>

Listing 17

A Predictable example (see Listing 18).

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:Panel ID="PanelParent"  runat="server">         
        <asp:Panel ID="PanelChild" runat="server">         
            <asp:TextBox ID="txtEcho" runat="server" ClientIDMode="Predictable" />
        </asp:Panel>
    </asp:Panel>
</asp:Content>

Listing 18

And the output is shown in the Listing 19.

<div id="MainContent_PanelParent">           
  <div id="MainContent_PanelChild">           
    <input name="ctl00$MainContent$txtEcho" type="text" id="MainContent_txtEcho" />       
   </div>  
</div>

Listing 19

8. Chart control

Problem: You need to visualize data in a comprehensible way in a Web application, for example, a financial analysis report.

Solution: ASP.NET 4 introduces a new chart control with a set of features such as pie, area, range, points, data distribution and Ajax support. At runtime, the control generates an image (a .pgn file) that is referenced by the client-side.

Implementation strategy:

First step is to add a HTTP handler to your application in the web.config (see Listing 20).

<httpHandlers>
  <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
</httpHandlers>

Listing 20

Later, we add the Chart control to our page (see Listing 21).

<asp:Chart ID="Chart1" runat="server" />

Listing 21

First example.

The Web Form page is shown in the Listing 22.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="01SalesByDayOfWeek.aspx.cs" Inherits="_04WebAppCharting._01SalesByDayOfWeek" %><%@ Register
    Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div>
       <h3>Sales Reports (By days of the week)</h3>
    </div>
    <div>
    <asp:Chart ID="chrSalesByDayOfWeek" runat="server" Width="500" Height="350">
        <Series>
            <asp:Series ChartType="Area" Palette="EarthTones" ChartArea="MainChartArea"></asp:Series>
        </Series>
        <ChartAreas>
          <asp:ChartArea Name="MainChartArea" Area3DStyle-Enable3D="true">
          </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>
    </div>
</asp:Content>

Listing 22

The code-behind code for this page is shown in the Listing 23.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _04WebAppCharting
{
    public partial class _01SalesByDayOfWeek : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Random random = new Random();
            string[] listDaysOfWeek = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
            foreach (string dayOfWeek in listDaysOfWeek)
            {
                double totalSales = random.NextDouble() * 5000 + 1000;
                this.chrSalesByDayOfWeek.Series[0].Points.AddXY(dayOfWeek, totalSales);
            }
        }
    }
}

Listing 23

And the output report is shown in the Figure 1.

1.gif
Figure 1

Let's add the data to the series statically (see Listing 24).

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="02SalesByDayOfWeekStatic.aspx.cs" Inherits="_04WebAppCharting._02SalesByDayOfWeekStatic" %>
<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div>
       <h3>Sales Reports (By days of the week)</h3>
    </div>
    <div>
    <asp:Chart ID="chrSalesByDayOfWeek" runat="server" Width="500" Height="350">
        <Series>
            <asp:Series ChartType="Area" Palette="EarthTones" ChartArea="MainChartArea">
                <Points>
                    <asp:DataPoint AxisLabel="Sun" YValues="17" />
                    <asp:DataPoint AxisLabel="Mon" YValues="15" />
                    <asp:DataPoint AxisLabel="Tue" YValues="6" />
                    <asp:DataPoint AxisLabel="Wed" YValues="4" />
                    <asp:DataPoint AxisLabel="Thu" YValues="3" />
                    <asp:DataPoint AxisLabel="Fri" YValues="3" />
                    <asp:DataPoint AxisLabel="Sat" YValues="3" />
                </Points>
            </asp:Series>
        </Series>
        <ChartAreas>
          <asp:ChartArea Name="MainChartArea" Area3DStyle-Enable3D="true">
          </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>
    </div>
</asp:Content>

Listing 24

And the report output is shown in the Figure 2.

2.gif
Figure 2

9. Html Encoded Code Expressions

Context: Some ASP.NET sites (especially with ASP.NET MVC) rely heavily on using <%= expression %> syntax (often called "code nuggets") to write some text to the response.

Problem: When you use code expressions, it is easy to forget to HTML-encode the text. If the text comes from user input, it can leave pages open to an XSS (Cross Site Scripting) attack.

Solution: ASP.NET 4 introduces the following new syntax for code expressions: <%: expression %>. 

This syntax uses HTML encoding by default when writing to the response. This new expression effectively translates to the following: <%= HttpUtility.HtmlEncode(expression) %>.

For those cases, ASP.NET 4 introduces a new interface, IHtmlString, along with a concrete implementation, HtmlString. Instances of these types let you indicate that the return value is already properly encoded (or otherwise examined) for displaying as HTML, and that therefore the value should not be HTML-encoded again. For example, the following should not be (and is not) HTML encoded: <%: new HtmlString("<strong>HTML that is not encoded</strong>") %>.

10. Html Encoded Code Expressions

Context: ASP.NET MVC 1.0 ships a great deal of HTML helpers used in the view templates to generate the HTML output. For example, <%= Html.TextBox("ProductName", Model.ProductName) %> where the first parameter is the Name/ID of the HTML element and the second one is its value. This produces the following output <input id="ProductName" name="ProductName" type="text" value="Product1" />.

Problem: One of the features asked by the developers is to have strongly typed HTML helpers that use lambda expressions to reference the model, and in this way, we can detect compile-time errors and have a better IntelliSense support.

Solution: ASP.NET 4 introduces a set of strongly-typed HTML helpers following the naming convention Html.HelperNameFor(). The list is enumerated:
  • Html.TextBoxFor()
  • Html.TextAreaFor()
  • Html.DropDownListFor()
  • Html.CheckboxFor()
  • Html.RadioButtonFor()
  • Html.ListBoxFor()
  • Html.PasswordFor()
  • Html.HiddenFor()
  • Html.LabelFor()
  • Html.EditorFor()
  • Html.DisplayFor()
  • Html.DisplayTextFor()
  • Html.ValidationMessageFor()
Implementation strategy: An example is shown in the Listing 25.

<%= Html.TextBoxFor(model=>model.ProductName) %>

Listing 25

11. Better validation model (ASP.NET MVC)

Problem: Validating user-input and enforcing business rules/logic is a core requirement of most web applications.

Solution: ASP.NET MVC 2 includes a bunch of new features that make validating user input and enforcing validation logic on models/viewmodels significantly easier.  These features are designed so that the validation logic is always enforced on the server, and can optionally also be enforced on the client via JavaScript. ASP.NET MVC 2 is designed to take advantages DataAnnotation validation support built-into the .NET Framework as well as existing validation frameworks like Castle Validator or the EntLib Validation Library.

Implementation strategy

Let's illustrate this new feature with an example.

First step is to define the Contact entity in the Entities package. To enforce input validation rules, we're going annotate the Contact entity with the validation rules to be enforced whenever ASP.NET MVC engine performs binding operations within an application. These annotations for validation are in the System.ComponentModel.DataAnnotations assembly (see Listing 26).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace _05MvcAppValidationModel.Entities
{
    public class Contact
    {
        [Required(ErrorMessage="FirstName field is required")]
        [StringLength(50, ErrorMessage="FirstName must be under 50 characters")]
        public string FirstName { get; set; }
        [Required(ErrorMessage = "LastName field is required")]
        [StringLength(50, ErrorMessage = "LastName must be under 50 characters")]
        public string LastName { get; set; }
        [Required(ErrorMessage = "Age field is required")]
        [Range(0,120,ErrorMessage="Age must be between 0 and 120")]
        public int Age { get; set; }
        public string Email { get; set; }
    }
}

Listing 26

Next step is to add the ContactManagerController to handle the HTTP requests regarding the contacts (see Listing 27).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _05MvcAppValidationModel.Entities;

namespace _05MvcAppValidationModel.Controllers
{
    public class ContactManagerController : Controller
    {
        //
        // GET: /ContactManager/
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult Create()
        {
            Contact newContact = new Contact();
            return View(newContact);
        }
        [HttpPost]
        public ActionResult Create(Contact contact)
        {
            if (ModelState.IsValid)
            {
                return Redirect("/");
            }
            return View(contact);
        }
    }
}

Listing 27

And right-click on the Create action method and select the Add View option from the context menu (see Figure 3).

3.gif
Figure 3

The Create view is generated with fields and validation necessary to create a contact (see Listing 28).

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<_05MvcAppValidationModel.Entities.Contact>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
          Add a contact
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Add a contact</h2>
    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>
        <fieldset>
            <legend>Fields</legend>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.FirstName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.FirstName) %>
                <%: Html.ValidationMessageFor(model => model.FirstName) %>
            </div>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.LastName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.LastName) %>
                <%: Html.ValidationMessageFor(model => model.LastName) %>
            </div>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Age) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Age) %>
                <%: Html.ValidationMessageFor(model => model.Age) %>
            </div>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Email) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Email) %>
                <%: Html.ValidationMessageFor(model => model.Email) %>
            </div>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
     <% } %>
    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>
</asp:Content>

Listing 28

And finally, let's run the application and enter invalid values in the Add Contact screen and see the results (see Figure 4).

4.gif
Figure 4

You can also enable client-side validation (see Listing 29).

    <h2>Add a contact</h2>
    <!-- BEGIN. This block is required for client-side validation -->
    <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
    <% Html.EnableClientValidation(); %>
    <!-- END. This block is required for client-side validation -->

13 February 2011

Disabling button when performing some operation in ASP.NET

In ASP.NET Web Application when we submit some button to process information in server side, we can disable the button as long the server performing the operation and enable the same one response is back. Most of the developer use Button Enable and Disable properties to enable and disable the button. But as an good practice we can use Page.ClientScript.GetPostBackEventReference to achieve the same.



Button1.OnClientClick = ClientScript.GetPostBackEventReference(Button1, "") + "; this.value='Processing...';this.disabled = true;";
To do a quick test, you can write the below code block









  protected void Page_Load(object sender, EventArgs e)
  {
    Button1.OnClientClick = ClientScript.GetPostBackEventReference(Button1, "") + "; this.value='Processing...';this.disabled = true;";
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
        System.Threading.Thread.Sleep(10000);
  }
 
image

09 February 2011

How to change the Target Framework Version for a Visual Basic Project

I received the following warning message from Visual Studio while adding a reference in a Visual Basic project:

The Target Framework version for the project ‘XXXX’ is higher than the current Target Framework version. 
Would you like to add this reference to your project anyway?
This warning message makes complete sense since the Visual Basic project I was working on is targeting the .NET Framework 2.0 and the project that was being referenced targeted the .NET Framework 3.5.  The only issue was that I was not able to figure out how to change the Target Framework Version in a Visual Basic project.
In a C# project it is pretty easy; right-click on the project and choose properties and in the Application tab (vertical tab) you will see the Target Framework dropdown box:

But in Visual Basic it is hidden.  Here is where I found how to change the Target Framework version after searching for a bit:

Right-click on the project and choose properties and then select the Compile tab (vertical tab).


At the bottom of the Project > Compile tab you will find an Advanced Compile Options… button. 

In this dialog called the Advanced Compiler Settings is where you will find how to change the Target Framework version for Visual Basic.

06 February 2011

Configuring IIS6 to host your Web Site On (Windows Server 2003)

Internet Information Services 6 (IIS6) is the latest version of Microsoft's web server bundled with Windows Server 2003. IIS6 will allow you to quickly deploy any number of web-based Intranets, Extranets or Web Sites.


1. Installing IIS6 on Windows Server 2003:

If using the new style Start menu: Click on “Start”, “Control Panel”, “Add or Remove Programs” and select the “Add/Remove Windows Components” tab on the left-hand side.
If using the "Classic" style Start menu: Click on “Start”, “Settings”, “Control Panel”, “Add or Remove Programs” and select the “Add/Remove Windows Components” tab on the left-hand side.
In the “Windows Components Wizard”, highlight the “Application Server” and press the “Details” button. The screen below will be displayed (Fig 1).
                                                Figure 1 - The Application Server Screen
First, check “Application Server Console” and “ASP.NET”, then highlight the “Internet Information Services (IIS)” option and press “Details” (as shown in Fig 1 above).
                                    Figure 2 - The Internet Information Services (IIS) Screen
On the next screen (Fig 2 above) we highlight “World Wide Web Services” and press the “Details” button to bring us onto the last screen.
                                           Figure 3 - The World Wide Web Service Screen
I make extensive use of ASP files in my Intranet, so I need to select the “Active Server Pages” option (shown in Fig 3 above). At this point, you may wish to check through the other options available in the list and select them if they apply to your site. Don't worry if you are unsure, you can always change these options later.
Click “OK” to close each window and “Next” to install the newly-selected components. You will be asked to insert your Windows Server 2003 disk. Click “Finish” once the installation is complete.
You have now installed Internet Information Services 6.
                                              Figure 4 - Where to find the IIS Manager
2. Where to find the IIS Manager:

If you are using the new style Start menu, you can reach the Internet Information Services console by clicking “Start”, “Administrative Tools” and selecting “Internet Information Services (IIS) Manager” from the list in figure 4 above.
If you are using the “Classic” style Start Menu, you can reach the console by clicking “Start”, “Programs”, “Administrative Tools” and select “Internet Information Services (IIS) Manager” from the list in figure 4 above.
                                      Figure 5 - Internet Information Services (IIS) Manager
 3. Internet Information Services (IIS) Manager:

IIS5 users will notice two new additions to the IIS Manager. The First is the “Web Service Extensions” screen which provides control over which file extensions should be allowed or blocked. The second is the “Application Pools” screen which allows applications to run in isolated memory “Pools” meaning that if one application has problems, others are not affected.
                                               Figure 6 - Creating a New Web Site in IIS 6 4. Creating a New Site in IIS 6:

The first task is to disable the “Default Web Site”. To do this, select the “Web Sites” item in the left-hand pane then right-click on the “Default Web Site” item in the right-hand pane and select “Stop” from the list. The Default Web Site is now stopped.
Next, we need to create a new site to host our web content. To do this, right-click on “Web Sites” in the left-hand pane and select “New” and “Web Site…” as shown above in figure 6.

When the “Welcome to the Web Site creation Wizard” screen appears, click next.

                                                              Figure 7 - Name your Site
5. Name your Site:

Simply enter in a name for your site in the box and press "Next" as shown in Figure 7 above. This name is for reference only and should help you locate your site in IIS Manager.
                                                   Figure 8 - Changing the IP Address
6. Method of Access (using an IP address - Recommended):

This method simply uses the IP address (or one of the IP addresses if more than one are configured) of the web site to allow users to reach the site.

To use this method, all you need to do is select an/the available IP number from the list and leave the port settings on 80.

To reach the site, visitors will have to type the IP address http://192.168.200.4 into their web browser. They can also type the machine name in eg. http://suk-sys4
                                                       Figure 9 - Changing the Port number
7. Method of Access (using Port numbers):

In this case, no additional IP numbers have been assigned to the Server, so all we have to work with is 192.168.200.4. Unfortunately, this is already being used by another site so we are going to host this particular Web Service from Port 81 instead of 80. To view the site, users will have to enter http://192.168.200.4:81 (the colon denotes the port number).
                                                   Figure 10 - Changing the Host Header
8. Method of Access (using Headers):
Headers allow multiple sites to run from one IP address and through one port (80). The advantage of using "Host Headers" is that a large number of sites can be hosted through just 1 external IP address.

The request is interpreted by the server and the visitor is directed to the correct site based upon the URL they entered and NOT just the IP address it was translated to by the DNS server.

This means that you need to add the URL (excluding http://) in the "Host Header" box as shown above. In the case of my Intranet, I am entering "intranet.simongibson.com" so visitors will type http://intranet.simongibson.com to access the site.

However, this will mean that a DNS entry must exist for "intranet.simongibson.com" to be translated to the correct IP address and route to our web server. This will therefore mean that you must also make this change to your DNS server if you plan to use this method.

Before you begin working with Host Headers it is recommended that you correctly configure a DNS server. To go to the DNS Configuration tutorial on this site, click here.
                                               Figure 11 - The Web Site Home Directory
9. Defing your site's home directory and who can access it:

Once you have defined which method of access you wish to use, you need to point IIS at your default home directory. This is pretty self-explanatory and in this case is c:\Inetpub\intranet_live which is the sub-directory 'intranet_live' located in the 'Inetpub' directory with the C: drive on the web server (SUK-SYS4).

Notice that I have unchecked 'Allow anonymous access to this Web Site'. This will ensure that any visitor will have to enter their username, password and Domain to access the site from outside the building (ideal for an Extranet).

If you do not plan to make your Intranet available through the Internet then it is probably best to leave this box checked.
                                               Figure 12 - Web Site Access Permissions
10. Defining Access Permissions:

The options shown in Fig.6 above are the default settings for IIS. These are fine for our Intranet. You can set up seperate access permissions for sub-directories later, such as a cgi-bin. See 'Configuring Active Perl' on the 'Intranet/Extranet' main page for more information.
                                   Figure 13 - The completed IIS6 Manager with new web site
11. The completed configuration of IIS:

If you have followed these instructions correctly, your IIS dialogue box should look something like Fig 13 (above). You can add as many extra web sites as you like, but bear in mind the access method you plan to use (check section 6, 7and 8).

To make sure your site is operational, make sure you have a valid default.asp file in the home directory and enter the preconfigured access method (http://192.168.200.4/, http://192.168.200.4:81/ or http://intranet.simongibson.com/).