Thursday, January 19, 2012

New features in ASP.Net 4.0

1. Adding MetaKeyword and MetaDescription Using Page objectEvery search engine will look for Meta Keywords tags to know more information of the page contents.Meta keywords and description are most important components of a page.
ASP.Net 4.0 added 2 new properties on the Page object to let you define the Meta keywords and Description.
Here is the code:

protected void Page_Load(object sender, EventArgs e)
{
Page.MetaKeywords = "asp.net,C#";
Page.MetaDescription = "This is an asp.net site that hosts asp.net tutorials.";
}

OR

<%@ Page Language="C#" AutoEventWireup="true" MetaKeywords="asp.net" MetaDescription="asp.net tutorials" CodeFile="Default.aspx.cs" Inherits="_Default" %>


2.ViewStateMode :ASP.Net 3.x, we have EnableViewState property both at Page level and server control level to control the view state. Disabling the ViewState at page level will disable the viewstate to all the page controls. In order to improve the performance, one need to switch off the viewstate for individual control for which saving the viewstate is not necessary and hence disabling viewstate at page level is not a suitable option. To overcome this difficulty, asp.net 4.0 added a new property to Page object and controls called ViewStateMode.
This property has 3 values:
1. Enabled
This value will enable the view state. This is the default value for the Page object.
2. Disabled
This value will disable the viewstate
3. Inherit
This value will make the control to inherit the setting of the parent. This is the default value for a control.
With this property, we can disable the viewstate for the page and enable it for the control if only required.
Consider the following code,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ViewStateTest.aspx.cs" ViewStateMode="Disabled" Inherits="ViewStateTest" %>









CodeBehind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Text = "Text Assigned in CodeBehind";
Label2.Text = "Text Assigned in CodeBehind";
}
}

When the page executed we will get the following output:
Text Assigned in CodeBehind
Text Assigned in CodeBehind

When the button is clicked:
Text Assigned in CodeBehind
Default Text

Since, the viewstate is disabled at page level (Page object) and the viewstate is enabled for “Label1” we will get the above output on Button click. Please note that if we have not set the value, the default will be “inherit” (for Label2).

3. CSS Improvements
The other major improvements in ASP.Net 4.0 are on the way the HTML rendered to the client browser. The rendered HTML will be complaint with latest HTML standards.
Few things will be:
1. A new property called controlRenderingCompatibilityVersion is added to the Pages element of configuration files.

pages controlRenderingCompatibilityVersion="3.5|4.0"/>

The value “3.5” indicates the controls are rendered in legacy ways. The value “4.0” indicates the control will be rendered with the latest improvements of 4.0 framework.

2. Till 3.5 framework, setting “Enabled=false” for any control will render disabled attributes in the HTML for the control. According to the latest HTML standard the disabled attribute should be set only for INPUT tags. ASP.Net 4.0 addresses this issue by rendering disabled attribute to only INPUT tags and a CSS class called “aspNetDisabled” to other controls to disable it when controlRenderingCompatibilityVersion property is set to “4.0”.

Below you can find a HTML rendered for a Label and TextBox control when controlRenderingCompatibilityVersion property is set to “4.0”.

Label
<input name="TextBox1" type="text" id="TextBox1" disabled="disabled" />

3. From ASP.Net 2.0, the hidden fields generated for viewstate are packed inside a div tag for XHTML standard. This leads a small white space on the page. ASP.Net 4.0 now includes this div with a css class “aspNetHidden”. We can now define the class to prevent the white space by setting border to 0.
4. Menu controls render markup that is semantically correct and compliant with accessibility guidelines.
5. Validation controls do not render inline styles.

4. RabioButtonList and CheckboxList Changes
From ASP.Net 4.0, the RadioButtonList and CheckBoxList controls includes 2 more value fro the property RepeatLayout.
1. OrderedList
The radiobutton/checkbox is rendered as li elements within an ol element.
2. UnorderedList
The radiobutton/checkbox is rendered as li elements within an ul element.

5. Improvements in Code Expressions
There is a new code expression syntax that is being shipped with ASP.Net 4.0. We commonly use the following syntax to output a value from aspx page called code expression.

<%= HTMLOutput %>
There is a new code expression releasing with ASP.Net 4.0 version that will html encode the value.

%: HTMLOutput %>

The above code expression will output the value after doing html encode. i.e. the above expression is equivalent to

%= HttpUtility.HtmlEncode(HTMLOutput) %>

6.Permanent Redirection with HTTP 301 Moved Permanently ResponseWhen we move our internet page to a different location, it is always required to send “HTTP 301 Moved Permanently” response to the search engines to hold back the page rank before redirecting to the new page. Understanding this need asp.net 4.0 includes a new method to do redirection called RedirectPermanent().

Refer below:
Response.RedirectPermanent("new url")


7. Optional ParametersTo implement optional parameters, we used to create overloaded functions before ASP.NET 4 however now optional parameters are no more a restriction in C#. Like VB, optional parameters must be mentioned last. For example:
public void FunctionOptionalParam(string Name, int Age, string Country = "") and we can call them without mentioning the value for the optional parameter.
FunctionOptionalParam("My Full Name",20);

8. Named ParametersNamed parameters allow you to ignore the parameter order and mention parameters with names in a different order. For example:

public void FunctionNamedParam(int x, int y , int z)

On function call, it would be:

FunctionNamedParam(x:1, z:3, y:2)

Although we are sending a value for the parameter z before its order in the function declaration, but these would be equal to x=1, y=2, z=3.

9. Dynamic Lookup
We can use dynamic as object of any type. If there is any error on its usage, we would get it on runtime only. For example:

dynamic integerValue = 1;
dynamic stringValue = " a string";
dynamic Result = integerValue + stringValue;

Output of this would be: 1 a string.

10. Compressing Session Values

ASP.NET session out-of-process state values are saved in a database or on the server. These are saved in a serialized format. Bigger session values consume more resources to be sent to the server. Now, those can be compressed with a new built-in property compressionEnabled. This attribute for the sessionState element can be mentioned in the web.config, like this:

mode="SQLServer"
stateConnectionString="connectionstring goes here"
compressionEnabled="true"/>

This option would be available for out-of-process sessions.


11. Add Reference Dialog

In previous versions of Visual Studio, on opening the Add Reference dialog box, it will take some time to load .NET assembly files initially till the time it loads all the references from the GAC. In VS2010, on opening Add Reference, it opens the Projects tab by default.



If you click on the .NET or COM tab by mistake, you still have an option of canceling it before it loads all the assemblies. So the VS IDE does not freeze like before.

No comments: