You are on page 1of 3

Chapter 1 Introducing ASP.

NET MVC 4
2
ASP.NET includes the following features:
A page and controls framework
The ASP.NET compiler
Security infrastructure
State-management facilities
Application configuration
Health monitoring and performance features
Debugging support
An XML web services framework, which was later superseded by Windows Communicati
on
Foundation (WCF)
An extensible hosting environment and application lifecycle management
An extensible designer environment
So, we have a rich and powerful environment in which we can build our web applic
ations by using the
language of our choice and it s free. In this environment, we can build applications
even with Notepad, but a more
sophisticated tool would increase our productivity by doing a lot of work for us
; yes, I m talking about Microsoft Visual
Studio. There are paid versions of Visual Studio, but if money is a problem, the
n you can use the free Visual Web
Developer Express.
One of the major benefits of ASP.NET is the change from interpreted code, previo
usly used for Classic ASP
(the programming model before ASP.NET), to compiled code, allowing web applicati
ons to have better performance.
When our code in the application is first compiled by the high-level language (C
#, VB.NET, etc.) compiler, it generates
Common Intermediate Language (CIL), which is commonly referred as Microsoft Inte
rmediate Language (MSIL)
code (an assembly language supercharged with lots of vitamins and minerals). The
MSIL code is later taken by the
.NET runtime to generate native machine code.
Web applications created with ASP.NET are executed by the .NET Framework, not th
e operating system.
This makes our applications type-safe and has the advantage of automatic memory
garbage collection. Additionally,
the .NET Framework provides structured error handling and multithreading support
. Finally, information about
classes, members, and all of our code in general is stored as metadata in the as
semblies generated at compile time.
To deploy ASP.NET applications, you can use one of the different techniques avai
lable, such as Web Deploy or, the
simplest method, a file copy to the server. Deploying ASP.NET applications is a
fairly simple process considering that
normally the .NET Framework is already installed on the server (and if not, it c
an be bundled with our applications).
After our applications are in the server, we need to setup Microsoft s web server,
Internet Information Services (IIS),
which will host all ASP.NET applications and serve the applications to end users
.
It is important to note that ASP.NET is fully object-oriented (OO), meaning that
not only the code we write but
also the code supporting ASP.NET is OO. Your code will have full access to all o
bjects in the .NET Framework, and you
can also implement all the goodies of an OO environment (inheritance, polymorphi
sm, and encapsulation).
ASP.NET and the .NET Framework have come a long way since ASP.NET s first release,
version 1.0, and the
minor update release 1.1. Version 2.0, released in 2005, added richer functional
ity with new controls, master pages,
themes and skins, web parts, full pre-compilation, and many more features. A yea
r later, version 3.0 added Windows
Communication Foundation (WCF), Windows Presentation Foundation (WPF), Windows W
orkflow Foundation (WF),
and Windows CardSpace. In .NET 3.5 Microsoft added even more controls and ASP.NE
T AJAX was already built into
the framework, WCF added support for RSS, JSON, POX and partial trust. .NET 3.5
Service Pack 1 introduced Dynamic
Data along with improvements for AJAX, JavaScript combining and new namespaces.
Version 4.0, released in 2010,
added a new set of features, such as extensible output caching, jQuery as the de
fault JavaScript library, routing in the
framework, a much better ViewState control, and a lot of improvements to existin
g functionality. Finally, the latest
version, 4.5, released in August 2012, includes asynchronous operations on HTTP
requests, responses, modules, and
handlers, strongly typed data controls, model binding, unobtrusive validation, a
nd HTML5, among other features.
Chapter 1 Introducing ASP.NET MVC 4
3
ASP.NET Web Forms
ASP.NET Web Forms allows you to create web applications in the same way you woul
d create a traditional Windows
Forms application. This means that you have a control-based interface and the fo
llowing two files for each page:
A user interface (UI) file: Includes the markup and is where you design how your
page will look
and which controls it will use. This file has the extension .aspx.
A code-behind file: Includes the code that handles events and interactions of al
l the objects
in the page (this code could be included on the preceding .aspx page, but normal
ly is in a
separate file). This file has an extension associated with the programming langu
age, either
aspx.cs for C# or aspx.vb for VB.NET.
Whenever ASP.NET processes a page, the page passes through several stages, each
of which raises different events
to handle the processing of the page and its controls. You write code to handle
these events and thus respond to
various actions related to the processing of a page. For example, you might writ
e code that gets called when the user
clicks a button. When a page is first requested, you often have to initialize da
ta and controls. However, when the page
posts back, you don t need to run this code.
A postback happens when a control on the page raises an event that must be handl
ed by the server. View state
is the information about the page control s status. After each postback, the page
view state is modified with the new
statuses of all the controls in the page. As a default, the page view state is s
tored in a hidden field inside each page
(see Figure 1-1), and its scope and lifetime are limited to the page it belongs
to.
Figure 1-1. The view state s hidden field
The main use of view state is to preserve form information across postbacks. Vie
w state is turned on by default
and normally serializes the data in every control on the page regardless of whet
her it is actually used during a postback.
This behavior can be modified, however, as view state can be disabled on a per-c
ontrol, per-page, or server-wide basis.
Also, as a security measure, no sensitive information should be stored in view s
tate because the serialized string can be
easily deserialized.
To work effectively with Web Forms, it is very important to understand the page
life-cycle events and how they
are processed. Table 1-1 lists these events and the effect they have on the page
and its controls.
Table 1-1. Page Life-cycle Events
Event Description
PreInit This is the first real event you might handle for a page. You typically
use this event only if
you need to dynamically (from code) set values such as master page or theme.
This event is also useful when you are working with dynamically created controls
for a
page. You want to create the controls inside this event.
Init This event fires after each control has been initialized. You can use this
event to change
initialization values for controls.
InitComplete This event is raised after all initializations of the page and its
controls have been completed.
PreLoad This event fires before view state has been loaded for the page and its
controls and before
postback processing.
(continued)

You might also like