Tuesday, February 24, 2009

asp.net

How ASP .NET different from ASP?
Difference between ASP.NET and ASP?

1. Scripting is separated from the HTML, Code is compiled as a DLL, these DLLs can be executed on the server.
2. ASP is interpreted. ASP.NET Compiled event base programming.Control events for text button can be handled at client java script only. Since we have server controls events can handle at server side.
3. More error handling.
4. ASP .NET has better language support, a large set of new controls and XML based components, and better user authentication.
5. ASP .NET provides increased performance by running compiled code.
6. ASP .NET code is not fully backward compatible with ASP.
7. ASP .NET also contains a new set of object oriented input controls, like programmable list boxes, validation controls. A new data grid control supports sorting, data paging, and everything you expect from a dataset control. The first request for an ASP.NET page on the server will compile the ASP .NET code and keep a cached copy in memory. The result of this is greatly increased performance.
8. ASP .NET is not fully compatible with earlier versions of ASP, so most of the old ASP code will need some changes to run under ASP .NET. To overcome this problem, ASP .NET uses a new file extension ".aspx". This will make ASP .NET applications able to run side by side with standard ASP applications on the same server.

Can we run both ASP and ASP.Net on same server?
Yes. They will run side by side without affecting each other’s working.

Can ASP pages and ASP.Net pages share session variables?
No. Both support session variables but the session variables are not shared across the two platforms.

Can ASP.NET still recognize the Global.asa file?
Asp.Net does not recognize the standard ASP global.asa file. Instead it uses a file named global.asax with the same name – plus additional functionality.


What methods are fired during the page load?
Various stages of page load lifecycle.
What is the order of events in a web form?
Init () - when the page is instantiated.
Load () - when the page is loaded into server memory.
PreRender () - the brief moment before the page is displayed to the user as HTML. Unload () - when page finishes loading.

What is the process-flow for ASP.NET?
1. User requests an ASPX page in the browser.
2. An HTTP request is sent to IIS.
3. The isapi filter (xspisapi.dll) intercepts the request and passes the request on to the XSP worker process (xspwp.exe).
4. The worker process takes care of handling the request to the appropriate HTTPModules and finally to HTTPHandler as defined in the configuration files.
5. The ASPX page is read from the HD or cache and the server code is loaded into memory and executed.
6. The server side code outputs normal HTML which is handled back through the chain of modules and eventually to IIS, which sends the response back to the client’s browser.
7. If the user clicks or otherwise acts on an HTML element (say, a textbox) that has a server side event handler, the form is posted back to the server with hidden form fields containing information as to what control and event occurred. Some controls do not automatically post by default, but wait for a button_click event to post. This is configurable by the developer.
8. The ASPX page is again loaded into memory using the same sequence of events as before, but this time ASP.Net reads in the hidden form field data and automatically triggers the appropriate _OnChange, OnClick and other appropriate event handlers.
9. Resulting HTML is sent to the browser.
10. The process continues in a continual “Post Back” cycle.

Describe the role of inetinfo.exe, aspnet_isapi.dll andaspnet_wp.exe in the page loading process?
inetinfo.exe is the Microsoft IIS server running, handling ASP.NET requests among other things. When an ASP.NET request is received (usually a file with .aspx extension),the ISAPI filter aspnet_isapi.dll takes care of it by passing the request to the actual worker process aspnet_wp.exe.

What is the difference between inline and code behind?
Inline code is written along side the HTML in a page. There is no separate distinction between design code and logic code.
Code-behind is code written in a separate file and referenced by the .aspx page.
Code Behind is relevant to Visual Studio.NET only.

What is the difference between Server.Transfer and Response.Redirect?
Server.Transfer: It transfers execution from the first page to the second page on the server.
All the state information that has been created for the first (i.e. calling) ASP page will be transferred to the second (i.e. called) page.
When second ASP page completes its tasks, you do not return to the first ASP page. All these happen on the server side, browser is not aware of this.
The benefit of this approach is one less round trip to the server from the client browser.
Response.Redirect: This tells the browser that the requested page can be found at a new location. The browser then initiates another request to the new page loading its contents in the browser.
Hence, there is roundtrip between client and server.
The redirect message issue HTTP 304 to the browser and causes browser to got the specific page.

Should validation (did the user enter a real date) occur server-side or client-side? Why?
Client-side. This reduces an additional request to the server to validate the users input.

What’s the difference between Response.Write () and Response.Output.Write ()?
The latter one allows you to write formatted output.

Suppose you want a certain ASP.NET function executed on MouseOver over a certain button. Where do you add an event handler?
It’s the Attributes property, the Add function inside that property. So
btnSubmit.Attributes.Add ("onClick","someClientCode () ;")

What does the "EnableViewState" property do? Why would I want it on or off?
It enables the viewstate on the page. It allows the page to save the users input on a form.

What is ViewState? ViewState represents the state of a page when it was last processed on the web server. It holds the values of a control that has been dynamically changed.

Briefly describe different techniques for ASP.NET State Management? State management is the art of retaining information between requests. State Management is of 2 types.
1. Client side state management
A) View State
B) Query String
C) Hidden Fields
D) Cookies
2. Server side state management
A) Session
B) Application
C) Cache

What is the lifespan for items stored in ViewState? Item stored in ViewState exist for the life of the current page. This includes postbacks (to the same page).

What is Postback?
When an action occurs (like button click), the page containing all the controls within the tag performs an HTTP POST, while having itself as the target URL. This is called Postback.

What is smart Navigation? The cursor position is maintained when the page get refreshed due to server side validation and the page get refreshed.

What are the different types of Session state management options available with ASP.NET? ASP.NET provides In-Process and Out-of-Process state management. In-Process stores the session in memory on the web server. This requires the a "sticky-server" (or no load-balancing) so that the user is always reconnected to the same web server. Out-of-Process Session state management stores data in an external data source. The external data source may be either a SQL Server or a State Server service. Out-of-Process state management requires that all objects stored in session are serializable.

How do you turn off SessionState in the web.config? By simply adding the httpmodule tag in web.cofig file

How can I kill a user Session? Call session.abandon

List the ASP.NET validation controls?
RequiredFieldValidator
RangeValidator
CompareValidator
RegularExpressionValidator
CustomValidator
ValidationSummary

What data type does the RangeValidator control support?
Integer, String and Date.

Name two properties common in every validation control?
ControlToValidate property and Text property.

Which control would you use if you needed to make sure the values in two different controls matched? CompareValidator Control

Can you give an example of what might be best suited to place in the Application_Start and Session_Start subroutines?
This is where you can set the specific variables for the Application and Session objects.

Can you edit data in the Repeater control?
No, it just reads the information from its data source

What is Data Binding? Data binding is a way used to connect values from a collection of data (e.g. DataSet) to the controls on a web form. The values from the dataset are automatically displayed in the controls without having to write separate code to display them.

Describe Paging in ASP.NET.
The DataGrid control in ASP.NET enables easy paging of the data. The AllowPaging property of the DataGrid can be set to True to perform paging. ASP.NET automatically performs paging and provides the hyperlinks to the other pages in different styles, based on the property that has been set for PagerStyle.Mode.

What is caching and the different techniques available for caching?
Caching is a concept of storing frequently used data into temporary memory. It provides data for immediate access to requesting program calls.
The types of caching are :-
Page / output caching
On per page basis or on a per user control basis, letting you store part or whole page in a cache, and customize their expiration.
Fragment / Partial page output caching
No need to cache entire page, only caches static i.e. html of page.
Programmatic / Data caching
Take advantage of .Net runtime cache engine to store any data or object between responses.
Cache[“foo”]=bar
Retrieve value- bar=cache[“foo”]
Ex.
i.e. page does not change for some period of time, for 15 seconds.
If page is refreshed or loaded within the next 15 seconds then the web server will serve the same contents without needing to dynamically regenerate the page.

What is authentication and authorization? Authentication is the process of identification and validation of a user's credentials. After the identity is authenticated, a process called authorization determines whether that identity has access to a particular resource.

Whenever a user logs on to an application, the user is first authenticated and then authorized. The application’s web.config file contains all of the configuration settings for an ASP.NET application. It is the job of the authentication provider to verify the credentials of the user and decide whether a particular request should be considered authenticated or not. An authentication provider is used to prove the identity of the users in a system. ASP.NET provides three ways to authenticate a user:
1 Forms authentication
2 Windows authentication
3 Passport authentication

Forms Authentication - This authentication mode is based on cookies where the user name and the password are stored either in a text file or the database. After a user is authenticated, the user’s credentials are stored in a cookie for use in that session. When the user has not logged in and requests for a page that is insecure, he or she is redirected to the login page of the application. Forms authentication supports both session and persistent cookies.

The following needs to be specified in the application’s web.config file for using Forms Based Authentication in ASP.NET:

Windows Authentication-This is the default authentication mode in ASP.NET. Using this mode, a user is authenticated based on his/her Windows account. Windows Authentication can be used only in an intranet environment where the administrator has full control over the users in the network. The following should be set in the web.config file to use Windows Authentication:

Windows authentication can be of the following types
1 Anonymous Authentication
2 Basic Authentication
3 Digest Authentication
4 Integrated Windows Authentication

Passport Authentication-Passport authentication is a centralized authentication service that uses Microsoft's Passport Service to authenticate the users of an application. It allows the users to create a single sign-in name and password to access any site that has implemented the Passport single sign-in (SSI) service. The following code shows how we can specify Passport Authentication in the web.config file:
ASP.NET also supports custom authentication. In such a case the authentication mode has to be specified as none in the web.config file as shown below:
Then we need to write our own custom authentication provider.

Authorization is the process of determining the accessibility to a resource for a previously authenticated user. Note that authorization can only work with authenticated users
The default authentication mode is anonymous authentication. There can be three types of authorization in ASP.NET. They are
1 URL Authorization
2 File Authorizations
3 Authorization based on ACLs

CONFIGURATION
The Web.config is an XML based configuration file for the entire application and resides in the application root. It provides the application wide settings for the entire application.

The Machine.config located at the "C:\WINDOWS\Microsoft.NET\Framework\vx.x.xxxx\CONFIG" directory, is used to apply configuration settings for any applications that are running in the entire system.


web.config
The XML based web.config file is used to specify the application wide settings for your entire application. The system wide settings are however stored in the machine.config file. There can be multiple config files in your application (one per each folder in your application) but only one machine.config file. Note that the settings specified in the Web.config file actually override the settings that are specified in the Machine.config file.
1) You can use the section of the section group to specify whether session and/or view state would be enabled or disabled for the web pages of the application.
2) The authentication and authorization sections in the web.config file allow you to specify the authentication mode applicable for your application and the authorization information for the authenticated users.
3) You can also specify the trace and globalization information using the respective sections in the web.config file.
4) You can specify the application wide error information using the section.
5) The section is the most widely used of all the sections in the web.config file and it typically stores the following.
· Database Connection Strings
· Server Name
· File Path
· Custom Key - Value Settings

To read the web.config file, use the code snippet given below.System.Configuration.Configuration wConfig =System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration ("/joydip");

You can read the machine.config file using the code snippet shown below.System.Configuration.Configuration mConfig =System.Web.Configuration.WebConfigurationManager.OpenMachineConfiguration ();


GLOBAL.ASAX
"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 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.
It is a collection of event handlers that you can use to change and set settings in your site. The events can come from one of two places - The HTTPApplication object and any HTTPModule object that is specified in web.confg or machine.config.

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.

HttpContext
Encapsulates all HTTP-specific information about an individual HTTP request.
Each time a Web server receives a request for an ASP.NET resource - be it an ASP.NET Web page, a Web service, or any other request that is mapped in IIS to the ASP.NET engine - an instance of the System.Web.HttpContext object is created. This class contains information about the request. For example, the HttpContext class has the "intrinsic" objects you're familiar with: Request, Response, Session, Application, Server, and Cache. HttpContext contains information about the current user making the request in its User property; it stores information about any errors that have occurred in its Errors property.
An HttpContext object will encapsulate specific details of a single HTTP request. Properties of this class include the Request object, the Response object, the Session object, and an AllErrors property which keeps an array of Exception objects accrued during the current request.

HTTP Handlers
Handlers are used to process individual endpoint requests. Handlers enable the ASP.NET framework to process individual HTTP URLs or groups of URL extensions within an application. Unlike modules, only one handler is used to process a request. All handlers implement the IHttpHandler interface, which is located in the System.Web namespace. Handlers are somewhat analogous to Internet Server Application Programming Interface (ISAPI) extensions.
HttpHandlers are the earliest possible point where we have access to the requests made to the web server (IIS). When a request is made to the web server for an ASP.NET resource (.aspx, .asmx, etc.), the worker process of the ASP.NET creates the right HttpHandler for the request which responds to the request. The default handlers for each request type that ASP.NET handles are set in the machine.config file. For example, all the requests made to ASP.NET (.aspx) pages are handled by System.Web.UI.PageHandlerFactory. So whenever an ASP.NET is requested at the web server, the PageHandlerFactory will fulfill the request.
Almost everything we do in an HttpHandler, we can simply do it in a normal .aspx page. Then why do we need HttpHandlers? First, the HttpHandlers are more reusable and portable than the normal .aspx pages. Since there are no visual elements in an HttpHandler, they can be easily placed into their own assembly and reused from project to project. Second, HttpHandlers are relatively less expensive than the PageHandler. For a page to be processed at the server, it goes through a set of events (onInit, onLoad, etc.), viewstate and postbacks or simply the complete Page Life Cycle. When you really have nothing to do with the complete page life cycle (like displaying images), HttpHandlers are useful, though the performance hit is negligible.

Creating an HttpHandler
To create an HttpHandler, we need to implement the IHttpHandler interface. The IHttpHandler contains one property and one method.

IHttpHandler interface
Public interface IHttpHandler
{
bool IsReusable { get; }
void ProcessRequest(HttpContext context);
}

The property IsReusable specifies whether the ASP.NET should reuse the same instance of the HttpHandler for multiple requests.
The ProcessRequest () method is where you actually implement the logic to handle the request.
After you create the HttpHandler, you have to map a path to the handler in the web.config.
A typical web.config configuration for the handler looks like the following.

The verb part is just what you want it to respond to (GET, POST, etc.). Path is the file or file extension you want it to respond to and Type is - ClassName, Assembly. For the above web.config setting, each request to the getAvatarImage.img will be sent to the handler and the ProcessRequest will be executed. When we use a new file extension like "getAvatarImage.img" or "getAvatarImage.do," we need to create IIS Extensions mapping for the new extensions.

We will be using the built-in extensions (.ashx) that are already mapped to ASP.NET so that we can avoid the necessity to modify the IIS extensions mapping. With .ashx extension, there is no requirement for registration in the web/machine.config.


HTTP Modules
Modules are called before and after the handler executes. Modules enable developers to intercept, participate in, or modify each individual request. Modules implement the IHttpModule interface, which is located in the System.Web namespace.
HTTP modules are .NET components that implement the System.Web.IHttpModule interface. These components plug themselves into the ASP.NET request processing pipeline by registering themselves for certain events. Whenever those events occur, ASP.NET invokes the interested HTTP modules so that the modules can play with the request.

No comments:

Post a Comment