Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Unit 5 - ASP.NET Applications

This document provides an overview of ASP.NET applications, detailing various file types, their locations, and purposes within the framework. It explains the significance of the /bin directory for component deployment, the concept of code-behind files for separating UI and logic, and the role of the Global.asax file in handling application and session events. Additionally, it outlines objectives for understanding these concepts and includes self-study questions.

Uploaded by

JINESH VARIA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Unit 5 - ASP.NET Applications

This document provides an overview of ASP.NET applications, detailing various file types, their locations, and purposes within the framework. It explains the significance of the /bin directory for component deployment, the concept of code-behind files for separating UI and logic, and the role of the Global.asax file in handling application and session events. Additionally, it outlines objectives for understanding these concepts and includes self-study questions.

Uploaded by

JINESH VARIA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

ASP.

NET Applications

5.0 Objectives
5.1 Introduction
5.2 ASP.NET File Types
5.3 The Application/ Bin Directory
5.4 Code-Behinds in ASP .NET
5.5 The Global.asax
5.6 ASP .NET Configuration
5.7 Summary
5.8 Check your Progress - Answers
5.9 Questions for Self – Study
5.10 Suggested readings

5.0 OBJECTIVES

After studying this chapter you will be able to –


 describe asp.net file types
 explain the use of bin directory
 discuss code-behinds
 describe global.asax
 explain the Asp.Net configuration.

5.1 INTRODUCTION
Web site applications can contain a number of file types, some supported and
managed by ASP.NET, and others supported and managed by the IIS server. Most of
the ASP.NET file types can be automatically generated using the Add New Item menu
item in Visual Web Developer.
5.2 ASP.NET FILE TYPES
File types are mapped to applications using application mappings. For
example, if you use double-click a .txt file in Windows Explorer, Notepad will probably
open, because in Windows, .txt file types are mapped by default to Notepad.exe. In
Web applications, file types are mapped to application extensions in IIS.

File Types Managed by ASP.NET


File type Location Description

Typically a Global.asax file


that contains code that derives
from the HttpApplication class.
This file represents the
.asax Application root.
application and contains
optional methods that run at
the start or end of the
application lifetime.

Application root or a
.ascx A Web user control file that
subdirectory.
ASP.NET Applications / 83
defines a custom, reusable
control.

A generic handler file that


Application root or a
.ashx contains code that implements
subdirectory.
the IHttpHandler interface.

An XML Web services file that


Application root or a contains classes and methods
.asmx
subdirectory. that are available to other Web
applications by way of SOAP.

An ASP.NET Web forms file


Application root or a (page) that can contain Web
.aspx
subdirectory. controls and presentation and
business logic.

A handler file used to manage


Web site administration
.axd Application root. requests, typically Trace.axd.

A browser definition file used


App_Browsers
.browser to identify the features of client
subdirectory.
browsers. .

Application root or a
.cd A class diagram file.
subdirectory.

A precompiled stub file that


point to an assembly
representing a compiled Web
.compile Bin subdirectory. site file. Executable file types
(.aspx, ascx, .master, theme
files) are precompiled and put
in the Bin subdirectory.

A configuration file (typically


Application root or a Web.config) containing XML
.config elements that represent
subdirectory.
settings for ASP.NET features.
.

Class source-code file that is


App_Code subdirectory, compiled at run time. The
or in the case of a code- class can be an HTTP Module,
behind file for an an HTTP Handler, a code-
.cs, .jsl, .vb
ASP.NET page, in the behind file for an ASP.NET
same directory as the page, or a stand-alone class
Web page. file containing application
logic.

Visual Studio project A project files for a Visual


.csproj, .vbproj, vjsproj Studio client-application
directory.
project. .

App_WebReferences An XML Web services


.disco, .vsdisco discovery file used to help
subdirectory.
locate available Web services.

A compiled class library files


.dll Bin subdirectory. (assembly). Note that instead
of placing compiled
ASP .NET / 84
assemblies in the Bin
subdirectory, you can put
source code for classes in the
App_Code subdirectory.
A license file. Licensing allows
control authors to help protect
Application root or a intellectual property by
.licx, .webinfo
subdirectory. checking that a user is
authorized to use the control.

A master page that defines the


Application root or layout for other Web pages in
.master
subdirectory. the application.

.mdb, .ldb App_Data subdirectory. An Access database file.

SQL database file for use with


.mdf App_Data subdirectory. SQL Server Express.

Application root or a An Indigo Messaging


.msgx, .svc Framework (MFx) service file.
subdirectory.
Application root or a A removing handler file.
.rem
subdirectory.
A resource file that contains
App_GlobalResources or resource strings that refer to
.resources, .resx App_LocalResources images, localizable text, or
subdirectory. other data.

Application root or a A system definition model


.sdm, .sdmDocument (SDM) file.
subdirectory.
A site-map file that contains
the structure of the Web site.
ASP.NET comes with a default
.sitemap Application root. site-map provider that uses
site-map files to easily display
a navigational control in a Web
page.

A skin file containing property


App_Themes settings to apply to Web
.skin controls for consistent
subdirectory.
formatting.

Visual Web Developer A solution file for a Visual Web


.sln Developer project.
project directory.
Application root or a A SOAP extension file.
.soap
subdirectory.

Source : http://msdn.microsoft.com/en-IN/library/2wawkw1c(v=vs.80).aspx

File Types Managed by IIS

File type Location Description


Typically a Global.asax file that contains optional
.asa Application root. methods that runs at the start or end of the ASP
session or application lifetime. .
Application root or a An ASP Web page that contains @ directives and
.asp
subdirectory. script code that uses the ASP built-in objects.
App_Data
.cdx A compound index file structure file for Visual FoxPro.
subdirectory.
Application root or a
.cer A certificate file used to authenticate a Web site.
subdirectory.

ASP.NET Applications / 85
Application root or a An Internet Database Connector file mapped to
.idc
subdirectory. httpodbc.dll.
.shtm,
Application root or a
.shtml, Mapped to ssinc.dll.
subdirectory.
.stm

Static File Types :

IIS serves static files only if their file-name extensions are registered in the MIME types
list. This list is stored in the MimeMap IIS metabase property for an application. If a file
type is mapped to an application extension, it does not need to be included in the
MIME types list unless you want the file to be treated like a static file. Typically,
ASP.NET source code file types should not be in the MIME types list because that
might allow browsers to view the source code.

The following table lists only a few of the registered file types.

File
Location Description
type

Application root or subdirectory, Style sheet files used to determine


.css
or App_Themes subdirectory. the formatting of HTML elements.

.htm, Static Web files written in HTML


Application root or subdirectory.
.html code.

Check your progress 5.2


Answer the following questions in 1-2 sentences.
a) Write any two asp.net file types.
………………………………………………………………………………………………
………………………………………………………………………………………………
b) What is static file type?
………………………………………………………………………………………………
………………………………………………………………………………………………

5.3 THE APPLICATION /BIN DIRECTORY


A problem with using the COM model for Web application components is that
those components must be registered before they can be used from a traditional ASP
application. Remote administration of these types of applications is often not possible,
because the registration tool must be run locally on the server. To make matters more
difficult, these components remain locked on disk once they are loaded by an
application, and the entire Web server must be stopped before these components can
be replaced or removed.
ASP.NET attempts to solve these problems by allowing components to be
placed in a well-known directory, to be automatically found at run time. This well-
known directory is always named /bin, and is located immediately under the root
directory for the application (a virtual directory defined by Internet Information Services
(IIS)). The benefit is that no registration is required to make components available to
the ASP.NET Framework application components can be deployed by simply copying
to the /bin directory or performing an FTP file transfer.
In addition to providing a zero-registration way to deploy compiled
components, ASP.NET does not require these components to remain locked on disk at
run time. Behind the scenes, ASP.NET duplicates the assemblies found in /bin and
loads these "shadow" copies instead. The original components can be replaced even
while the Web server is still running, and changes to the /bin directory are

ASP .NET / 86
automatically picked up by the runtime. When a change is detected, ASP.NET allows
currently executing requests to complete, and directs all new incoming requests to the
application that uses the new component or components.
5.4 CODE BEHINDS IN ASP.NET
Even though ASP.NET is the next version of ASP, it is more than just a next
version. ASP.NET is completely re-designed from the ground up and it provides a neat
object oriented programming model. An ASP.NET page is an object derived from the
.NET Page class. We all know that ASP.NET provides several new features and one
of the important features is the separation of code and content.
In today's ASP application, we have a mix of HTML and Scripts making the
code difficult to read, debug and maintain. ASP.NET relieves us from this problem by
promoting the separation of code and content. That is, the user interface and the user
interface programming logic need not necessarily be written in a single page. There
are two ways in which you can separate the code and content:

1) By using Code-behind files that are pre-compiled modules written in any of the
Microsoft .NET runtime compliant languages.
2.) By developing the frequently used page logic in to separate files called Pagelets
(also known as page controls) and by using them several times in your ASP.NET
pages.

As the title of the article indicates, I will explain the first mechanism that is
used to neatly encapsulate the UI functionality in to a separate pre-compiled module.
You will typically follow the steps below to develop an ASP.NET page that uses code-
behind pages:
1) Create the User Interface using the HTML and/or Web controls and store them
in an ASP.NET page with extension ".aspx".
2) Create the code-behind file using the .NET runtime classes that mimic the entire
ASP.NET page with the member variables that correspond to the HTML and/or
Web controls used in the ASP.NET page. The extension of code behind files will
vary depending on the language you use. Typical extensions will be .cs for C#,
.vb for VB7 and .js for JScript .NET
Now, we know that there are two separate files: one for the UI and the other
for the UI logic. But, how do we relate these two files so that the ASP.NET page
compiler will locate the code-behind file for the ASP.NET page. The glue between
these two files is provided through the Page Directives, which are used to specify
optional settings at the page level.

Creating the "Content"


Enough theory said about code-behind files. Let us get our hands on to the
actual coding of a code-behind file. The ASP.NET page that displays the Web UI can
be seen below. It uses a post-back form to query the user for username and
information:

<%@ Page language="c#" Codebehind="CodeBehind.cs"


Inherits="ASPPlus.CodeBehind" %>

<HTML><BODY>
<form action="CodeBehind.aspx" method=post runat="server">

<p align=center>Demonstration of Code Behind File</P>


<p align=center><asp:Label id=Message runat="server"></asp:Label></P>
<p>

<h5>Sign-In Form</h5>
Login: <asp:TextBox id=txtLogin Width=200 Runat=server />
<asp:RequiredFieldValidator ControlToValidate="txtLogin"
Display="Static" Font-Name="verdana,arial,sans-serif"
Font-Size="9pt" ErrorMessage="You must enter a Login."
runat="server" ID="RFVLogin" />

ASP.NET Applications / 87
<P>
Password: <asp:TextBox id=txtPassword Width=200 Runat=server
TextMode="Password" />
<asp:RequiredFieldValidator ControlToValidate="txtPassword"
Display="Static" Font-Name="verdana,arial,sans-serif"
Font-Size="9pt" ErrorMessage="You must enter a password."
runat=server ID="RFVPassword"> </asp:RequiredFieldValidator>

<P>
<asp:Button id=btnSignIn Runat="server"
Text="Sign In" onClick="btnSignIn_Click" />
</FORM>
</body></html>

The first line of the ASP.NET page is the page directive that specifies the name of the
code behind file and the actual class name inside that file. In our example, the file
name is CodeBehind.cs and the class name is ASPPlus.CodeBehind.

<%@ Page language="c#" Codebehind="CodeBehind.cs"


Inherits="ASPPlus.CodeBehind" %>

The namespace that I used for this code behind page is ASPPlus. Namespaces
provide the naming scope for the classes and is very convenient way to represent
hierarchy of classes. The rest of the code consists of HTML tags and Web Control
declaration tags. The main thing to note down in this page is the ID properties of the
controls. I will explain the importance of these IDs when we discuss the code-behind
file.

Check your progress 5.3 – 5.4


Answer the following questions in 1-2 sentences.
a) What is bin directory?
………………………………………………………………………………………………
………………………………………………………………………………………………
b) Explain code behind in Asp.net.
………………………………………………………………………………………………
………………………………………………………………………………………………

5.5 THE GLOBAL.ASAX


Introduction
The Global.asax file (also known as the ASP.NET application file) is an
optional file that is located in the application's root directory and is the ASP.NET
counterpart of the Global.asax of ASP. This file exposes the application and session
level events in ASP.NET and provides a gateway to all the application and the session
level events in ASP.NET. This file can be used to implement the important application
and session level events such as Application_Start, Application_End, Session_Start,
Session_End, etc. This article provides an overview of the Global.asax file, the events
stored in this file and how we can perform application wide tasks with the help of this
file.
What is the Global.asax file?
According to MSDN, "The Global.asax file, also known as the ASP.NET
application file, is an optional file that contains code for responding to application-level
events raised by ASP.NET." The Global.asax file is parsed and dynamically compiled
by ASP.NET into a .NET Framework class the first time any resource or URL within its
application namespace is activated or requested. Whenever the application is
requested for the first time, the Global.asax file is parsed and compiled to a class that
extends the HttpApplication class. When the Global.asax file changes, the framework
reboots the application and the Application_OnStart event is fired once again when the
ASP .NET / 88
next request comes in. Note that the Global.asax file does not need recompilation if no
changes have been made to it. There can be only one Global.asax file per application
and it should be located in the application's root directory only.
Events in the Global.asax file
The following are some of the important events in the Global.asax file.
Application_Init : The Application_Init event is fired when an application initializes the
first time.
Application_Start : The Application_Start event is fired the first time when an
application starts.
Session_Start : The Session_Start event is fired the first time when a user’s session
is started. This typically contains for session initialization logic code.
Application_BeginRequest : The Application_BeginRequest event is fired each time
a new request comes in.
Application_EndRequest : The Application_EndRequest event is fired when the
application terminates.
Application_AuthenticateRequest :
The Application_AuthenticateRequest event indicates that a request is ready to be
authenticated. If you are using Forms Authentication, this event can be used to check
for the user's roles and rights.
Application_Error : The Application_Error event is fired when an unhandled error
occurs within the application.
Session_End : The Session_End Event is fired whenever a single user Session ends
or times out.
Application_End : The Application_End event is last event of its kind that is fired
when the application ends or times out. It typically contains application cleanup logic.

Using the Global.asax file


The following code sample shows how we can use the events in the
Global.asax file to store values in the Application state and then retrieve them when
necessary. The program stores an Application and a Session counter in the
Application state to determine the number of times the application has been accessed
and the number of users currently accessing the application.
Example 1
using System;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
public class Global : HttpApplication
{
protected void Application_Start(Object sender, EventArgs e)
{
Application["appCtr"] = 0;
Application["noOfUsers"] = 0;
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Application.Lock();
Application["appCtr"] = (int) Application["appCtr"] + 1;
Application.UnLock();
}

protected void Session_Start(Object sender, EventArgs e)


{
ASP.NET Applications / 89
Application.Lock();
Application["noOfUsers"] = (int) Application["noOfUsers"] + 1;
Application.UnLock();
}
// Code for other handlers
}
After storing the values in the Application state, they can be retrieved using the
statements given in the code sample below.
Example 2
Response.Write("This application has been
accessed "+Application["appCtr"] + " times");
Response.Write("There are "+ Application["noOfUsers"] + " users accessing this
application");

5.6 ASP.NET CONFIGURATION


The ASP.NET configuration system features an extensible infrastructure that
enables you to define configuration settings at the time your ASP.NET applications are
first deployed so that you can add or revise configuration settings at any time with
minimal impact on operational Web applications and servers.

The ASP.NET configuration system provides the following benefits:

 Configuration information is stored in XML-based text files. You can use any
standard text editor or XML parser to create and edit ASP.NET configuration
files.

 Multiple configuration files, all named Web.config, can appear in multiple


directories on an ASP.NET Web application server. Each Web.config file applies
configuration settings to its own directory and all child directories below it.
Configuration files in child directories can supply configuration information in
addition to that inherited from parent directories, and the child directory
configuration settings can override or modify settings defined in parent
directories. The root configuration file named
systemroot\Microsoft.NET\Framework\versionNumber\CONFIG\Machine.config
provides ASP.NET configuration settings for the entire Web server.

 At run time, ASP.NET uses the configuration information provided by the


Web.config files in a hierarchical virtual directory structure to compute a
collection of configuration settings for each unique URL resource. The resulting
configuration settings are then cached for all subsequent requests to a resource.
Note that inheritance is defined by the incoming request path (the URL), not the
file system paths to the resources on disk (the physical paths).

 ASP.NET detects changes to configuration files and automatically applies new


configuration settings to Web resources affected by the changes. The server
does not have to be rebooted for the changes to take effect. Hierarchical
configuration settings are automatically recalculated and reached whenever a
configuration file in the hierarchy is changed.

 The ASP.NET configuration system is extensible. You can define new


configuration parameters and write configuration section handlers to process
them.

 ASP.NET help protect configuration files from outside access by configuring


Internet Information Services (IIS) to prevent direct browser access to
configuration files. HTTP access error 403 (forbidden) is returned to any browser
attempting to request a configuration file directly.

ASP .NET / 90
Check your progress 5.5-5.6
Answer the following questions in 1-2 sentences.
a) What is Global.asax?
………………………………………………………………………………………………
………………………………………………………………………………………………
b) Explain asp.net configuration.
………………………………………………………………………………………………
………………………………………………………………………………………………

5.7 SUMMARY
 Web site applications can contain a number of file types, some supported and
managed by ASP.NET, and others supported and managed by the IIS server.
Most of the ASP.NET file types can be automatically generated using the Add
New Item menu item in Visual Web Developer.
 In asp.net directory is always named /bin, and is located immediately under the
root directory for the application (a virtual directory defined by Internet
Information Services (IIS)).
 The Global.asax file (also known as the ASP.NET application file) is an optional
file that is located in the application's root directory and is the ASP.NET
counterpart of the Global.asax of ASP.
5.8 CHECK YOUR PROGRESS – ANSWERS

5.2
1. .asax, .ascx
2. If the file name, extensions are register in the MIME File types.

5.3-5.4
1. To solve the traditional problem of com object, Microsoft create a well known \
bin directory to store application file at run time.
2. Meaning of it means that your page designee is distinct from your coding.

5.5 – 5.6
1. It is a file used to declare and handle application / session level events, object
for ASP .NET website running on IIS web server.
2. ASP .NET configuration means now your website will work in term of – Role,
Authentication, and Authorization. These all are customizable and we find them
in web.config file>system.web.
ASP .NET configuration gives us easy way to manage our web page and other
type of administration. We can do it by code also.

5.9 QUESTIONS FOR SELF - STUDY


1. Explain the Asp.Net file types with examples.
2. Explain global .asax events.
3. Explain Asp.Net configuration benefits.
5.10 SUGGESTED READINGS
Beginning ASP.NET 4.5 in C#, By Matthew MacDonald


ASP.NET Applications / 91

You might also like