The global.asax file in ASP.NET

The global.asax file in ASP.NET as an application file, which is optional and allows us to write code that response to global application events raised by ASP.NET or by HttpModules. These events fire at various points during the lifetime of a web application, including when the application domain is first created.

The global.asax file resides in the root directory of an ASP.NET application. At run time, global.asax is parsed and compiled into a dynamically generated .NET Framework class derived from the HttpApplication base class.

The global.asax file looks very similar to a normal .aspx file. However, instead of containing HTML or ASP.NET tags, it contains event handlers that response to application events. Each ASP.NET application can have only one global.asax file. Once you place it in the appropriate website directory, ASP.NET recognizes it and uses it automatically.

The ASP.NET global.asax file can coexist with the ASP global.asa file.

Basic Application Events

S. No.
Event Handling Method
Description
1
Application_Start()
Application_Start() event occurs when the application starts, which is the first time it receives a request from any user. It doesn’t occur on subsequent requests. This event is commonly used to create or cache some initial information that will be reused later.
2
Application_End()
Application_End() event occurs when the application is shutting down, generally because the web server has restarted. You can insert cleanup code here.
3
Application_BeginRequest()
Application_BeginRequest() event occurs with each request the application receives, just before the page code is executed.
4
Application_EndRequest()
Application_EndRequest() event occurs with each request the application receives, just after the page code is executed.
5
Session_Start()
Session_Start() event occurs whenever a new user request is received and a session is started.
6
Session_End()
Session_End() event occurs when a session times out or is programmatically ended. This event is only raised if you are using in-process session state storage (the InProc mode, not the StateServer or SQLServer modes ).
7
Application_Error()
Application_Error() event occurs in response to an un-handled error.

IsPost Property in ASP.NET & HTTP GET and POST Methods

The HTTP protocol (used for web pages) supports a very limited number of methods (verbs) that are used to make requests to the server. The two most common ones are GET (which is used to read a page), and POST (which is used to submit a page). In general, the first time a user requests a page, the page is requested using GET. If the user fills in a form and then clicks a submit button, the browser makes a POST request to the server.

In web programming, it's often useful to know whether a page is being requested as a GET or as a POST so that you know how to process the page. In ASP.NET Web Pages, you can use the IsPost property to see whether a request is a GET or a POST. If the request is a POST, the IsPost property will return true, and you can do things like read the values of text boxes on a form.

Explain the services provided by the .Net Framework

The .NET Framework is a managed execution environment that provides a variety of services to its running applications. It consists of two major components: the Common Language Runtime (CLR), which is the execution engine that handles running applications; and the .NET Framework Class Library, which provides a library of tested, reusable code that developers can call from their own applications.
The services that the .NET Framework provides to running applications include the following:


  • Memory Management - In .NET Framework applications, the CLR is responsible for allocating and releasing memory and for handling object lifetimes on behalf of the application.
  • Common Type System - In the .NET Framework, basic types are defined by the .NET Framework type system and are common to all languages that target the .NET Framework. This fecilitates cross-language interoperability.
  • Extensive Class Library - Programmers can use a readily accessible library of types and their members from the .NET Framework Class Library, instead of writing vast amounts of code to handle common low-level programming operations.
  • Development Frameworks and Technologies - The .NET Framework includes libraries for specific areas of application development, such as ASP.NET for web applications, ADO.NET for data access, and Windows Communication Foundation for service-oriented applications.
  • Language Interoperability - Language compilers that target the .NET Framework emit an intermediate code named Common Intermediate Language (CIL), which, in turn, is compiled at run time by the Common Language Runtime. With this feature, routines written in one language are accessible to other languages, and programmers can focus on creating applications in their preferred language or languages.
  • Version Compatibility - With rare exceptions, applications that are developed by using a particular version of the .NET Framework can run without modification on a later version.
  • Side-by-Side Execution - The .NET Framework helps resolve version conflicts by allowing multiple versions of the common language runtime to exist on the same computer. This means that multiple versions of applications can also coexist, and that an application can run on the version of the .NET Framework with which it was built.
  • Multitargeting - By targeting the .NET Framework Portable Class Library, developers can create assemblies that work on multiple .NET Framework platforms, such as Windows 7, Windows 8, Windows Phone, and Xbox 360.

Glossary


Term
Definition
.NET Framework
The .NET Framework is a component of the Windows operating system that provides the programming model for building, deploying and running Web-based applications, smart client applications and Web services. The .NET Framework consists of the common language runtime (CLR) and a unified class library.
Active Directory
The Windows directory service that provides a unified, hierarchical view of complex networks.
ADO.NET
The suite of data access technologies included in the .NET Framework class libraries
ASP.NET
The development component for building server-based Web applications. An evolution of ASP into the .NET Framework.
Assembly
The primary building block—also the unit of deployment and versioning—of a .NET Framework application. An assembly includes an assembly manifest, which describes the contents of the assembly
C#
A new ECMA-approved programming language designed for the .NET Framework. C#, which is an evolution of C and C++, is type safe and object oriented. Because it is compiled as managed code, it benefits from the services of the common language runtime, such as language interoperability, enhanced security, and garbage collection.
Class Library, .NET Framework
A library of classes, interfaces, and value types that are included in the Microsoft .NET Framework and can be used from any CLS-compliant language. The .NET Framework class library provides access to system functionality and is designed to be the foundation on which .NET Framework applications, components, and controls are built.
Common Language Runtime (CLR)
The engine at the core of .NET Framework-managed code execution. The runtime supplies managed code with services such as cross-language integration, code access security, object lifetime management, and debugging and profiling support.
Common Language Specification (CLS)
A subset of .NET Framework features that are supported by a broad set of compliant languages and tools. CLS-compliant languages and tools are guaranteed to interoperate with other CLS-compliant languages and tools.
ECMA
A European standards body created in 1961. Internationally accredited ECMA has fast-track approval for ISO and is the forum for successful standards such as ECMAScript.
Evidence-Based Security
The .NET Framework introduces the concept of evidence-based security, referring to inputs to the security policy about code—such as from what site, security zone, or URL was an assembly obtained, what is its strong name, and whether it has a digital signature and from whom. Based on these and other answers—which can come from multiple sources depending on where the code is run—the appropriate security policy can be applied, and the appropriate permissions may be granted to the assembly..
Extensible Markup Language (XML)
A subset of Standard Generalized Markup Language (SGML) that is optimized for delivery over the Web. XML provides a uniform method for describing and exchanging structured data that is independent of applications or vendors.
Garbage Collection (GC)
The process of transitively tracing through all pointers to actively used objects to locate all objects that can be referenced and then arranging to reuse any heap memory that was not found during this trace. The CLR garbage collector also compacts the memory that is in use to reduce the working space needed for the heap.
HTTP
Hyper Text Transfer Protocol is a standard Internet protocol for transfer of information between servers and between clients and servers.
IDL
Interface Definition Language. A language used by applications to specify the various interfaces they intend to offer to other applications.
Microsoft intermediate language (MSIL)
A language used as the output of a number of compilers and as the input to a just-in-time (JIT) compiler, which produces native code. MSL defines an abstract, stack-based execution model.
JIT
An acronym for "just-in-time", a phrase that describes an action that is taken only when it becomes necessary, such as just-in-time compilation or just-in-time object activation.
Loosely Coupled Architecture
A distributed application in which you can change the implementation of one tier without affecting any of the other tiers. Contrast tightly coupled architecture.
Managed Code
Managed code supplies the metadata necessary for the CLR to provide services, such as memory management, cross-language integration, code access security, and automatic lifetime control of objects. All code based on MSIL executes as managed code.
Manifest
An integral part of every assembly that renders the assembly self-describing via metadata. The metadata describes which modules and resource files are part of a particular assembly, which types are exported, and which other assemblies are referenced. It also specifies which security permissions are required to run, what additional permissions are optionally requested, and what permissions the assembly refuses.
Metadata
Data (or information) about data. Many different systems use metadata—for example, type libraries in COM provide metadata and databases have schemas. In the CLR, metadata is used to describe assemblies and types. It is stored with them in the executable files, and is used by compilers, tools, and the runtime to provide a wide range of services. Metadata is essential for runtime type information and dynamic method invocation.
Native Code
Code that has been compiled to processor-specific machine code.
n-tier
System architecture that separates presentation, business logic, data access, and database (or other persistence mechanism) tiers.
Reflection
.NET Framework technology that allows you to examine metadata that describes types and their members. Reflection can be used to create, invoke, and access type instances at run time.
Serviced Component
The mechanism that enables COM+ services to be available to .NET Framework classes.
Side-by-Side Execution
The ability to run multiple versions of the same assembly simultaneously. This can be on the same computer or in the same process or application domain. Allowing assemblies to run side-by-side is essential to support robust versioning in the common language runtime. Side-by-side is also used to describe to describe two versions of the .NET Framework running simultaneously on the same computer.
SOAP
Simple Object Access Protocol, a W3C standard. A lightweight protocol for exchange of information in a decentralized, distributed environment. It is an XML-based protocol for exchanging structured and type information on the Web. The SOAP protocol contains no application or transport semantics, which makes it highly modular and extensible.
Tightly Coupled Architecture
A distributed application where a change to any tier affects some or all the other remaining tiers. Contrast loosely coupled architecture.
UDDI
Universal Description, Discovery, and Integration (UDDI) specification. An initiative that creates a global, platform-independent, open framework to enable Web service providers to advertise the existence of their Web services and for Web service consumers to locate Web services of interest.
Unmanaged Code
Code that was created without knowledge for the conventions and requirements of the .NET Framework. Unmanaged code executes in the .NET Framework environment with minimal services (for example, no garbage collection, limited debugging, and no declarative security).
Web forms
The ASP.NET page framework, which supports server-side controls that render HTML user interface on Web browsers.
Web services
A programming model that provides the ability to exchange messages in a scalable, loosely coupled, and platform-neutral environment using standard protocols such as HTTP, XML, XSD, SOAP, and WSDL. The SOAP-based XML messages exchanged between a Web service and its clients can be structured and typed, or loosely defined. The flexibility of using a text format such as XML enables the message exchange to evolve over time in a loosely coupled way. Because they are based on standard protocols and are platform neutral, Web services enable communication with a broad variety of implementations, platforms, and devices.
Web Services Description Language (WSDL)
An XML-based contract language for describing network services offered by a server.
Windows Forms
A rich Windows client library that encapsulates native Win32 APIs and exposes secure, managed classes for creating smart Windows client applications. The Windows Forms class library provides many controls, such as buttons, check boxes, drop-down lists, combo boxes, data grid, and others, that encapsulate user-interface and other client-side functionality.
Windows Management Instrumentation (WMI)
A component of the Windows operating system that provides management information and control in an enterprise environment using industry-wide standards.
WSDL
(see Web Services Description Language)
XML
(see Extensible Markup Language).
XML Schema Definition (XSD)
A W3C Recommendation that specifies how to formally describe the elements of an XML document. The schema can be used to verify the conformance of elements in an XML document.

Page Life Cycle - Guess the correct answer

In which stage of general page life cycle, 'IsPostBack' property is set?

1) Page Request
2) Start
3) Load
4) Validation
5) Rendering


Answer: Option 2: Start

Explanation: In the Start step, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property.

External Link: Page Life Cycle in ASP.NET

Virtual Methods / Method Overriding - Guess the Output

class A
{
    public virtual void ABC()
    {
        Console.WriteLine("A");
    }
}

class B : A
{
    public override void ABC()
    {
        Console.WriteLine("B");
    }
}

class C: B
{
    new public virtual void ABC()
    {
        Console.WriteLine("C");
    }
}

class X
{
    public static void Main()
    {
        A a = new C();
        B b = new C();
        C c = new C();

        a.ABC();
        b.ABC();
        c.ABC();
    }
}


Option 1: ABC
Option 2: BBA
Option 3: BBC
Option 4: AAB
Option 5: Error

Answer: Option 3: BBC

Explanation: While overriding, the type of object determine which method to be called. Method overriding is a feature that allows us to invoke functions (that have the same signatures) that belong to different classes in the same hierarchy of inheritance using the base class reference. C# makes use of two keywords: virtual and override to accomplish method overriding.

The method ABC() of base class A is declared as virtual, while the derived class's implementation of ABC() is decorated with override modifier. This enables C# to invoke methods like ABC() based on objects the reference variable refers to and not the type of reference that is invoking the function.

Hence in the above program when the instances "a" and "b" refers to the object of class C, the code goes to its base class B, since class B overrides the method ABC of its base class A, method ABC of class B is invoked in both the cases.

When create an object of type C and assign its reference to reference variable "c", ABC() method of class C is executed because the reference variable c refers to the object of class C.