Saturday, May 2, 2009

Frequently asked interview questions in ASP.Net

This article explains the frequently asked interview questions in ASP.Net with answers


1. How to bring COM Component Compatibility in ASP.NET?

When using single-threaded apartment (STA) COM components, such as components developed using Visual Basic, from an ASP.NET page, you must include the compatibility attribute AspCompat=true in an Page tag on the ASP.NET page, as shown in the following code example.
<%@ Page AspCompat="true" Language = "C#" %>

2. What is the difference between login controls in ASP.Net and Forms authentication?

Login controls are an easy way to implement Forms authentication without having to write any code. For example, the Login control performs the same functions you would normally perform when using the FormsAuthentication class

  • prompt for user credentials
  • validate them, and
  • issue the authentication ticket


But with all the functionality wrapped in a control that you can just drag from the Toolbox in Visual Studio. Under the covers, the login control uses the FormsAuthentication class (for example, to issue the authentication ticket) and ASP.NET membership (to validate the user credentials). Naturally, you can still use Forms authentication yourself, and applications you have that currently use it will continue to run.


3.Since there can be multiple ASP.NET configuration files on one computer, how does ASP.NET configuration handle inheritance?

ASP.NET integrates the settings in configuration files (the Machine.config and Web.config files) into a single inheritance hierarchy. With a few exceptions, you can place a Web.config file wherever you need to override the configuration settings that are inherited from a configuration file located at a higher level in the hierarchy.


We can have web config file at each folder or sub folder of the web application which overrides the higher level config. To learn more about web config hierarchy, click here


4. What is the difference between Web server control and HTML control?

Server Control

  • Runs at the serverY
  • ou prefer a Visual Basic-like programming model.
  • You are writing a Web Forms page that might be used by both HTML 3.2 and HTML 4.0 browsers.

HTML control

  • Runs in the Browser
  • You prefer an HTML-like object model.
  • You are working with existing HTML pages and want to quickly add Web Forms functionality. Because HTML server controls map exactly to HTML elements, they can be supported by any HTML design environment.
  • The control will also interact with client script.


HTML Server Controls, By default, HTML elements within an ASP.NET file are treated as literal text and you cannot reference them in server-side code. To make these elements programmatically accessible, you can indicate that an HTML element should be treated as a server control by adding the runat="server" attribute. You can also set the element's id attribute to give you way to programmatically reference the control. You then set attributes to declare property arguments and event bindings on server control instances. Sample controls are HtmlAnchor, HtmlButton, HtmlForm, HtmlLink etc.

5. Explain ASP.NET State Management?

States in ASP.Net is maintained at client and server sides.

Client-Based State Management Options

  • View state : The web is stateless. But in ASP.NET, the state of a page is maintained in the page itself automatically. The values are encrypted and saved in hidden controls. This is done automatically by the ASP.NET. This can be switched off / on for a single control
    This is done using EnableViewState property for each control.

  • Control state

  • Hidden fields ASP.NET allows you to store information in a HiddenField control, which renders as a standard HTML hidden field. A hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control.

<asp:hiddenfield id="ExampleHiddenField" value="Example Value" runat="server"/>

  • Cookies : A cookie is a small amount of data that is stored either in a text file on the client file system or in-memory in the client browser session. It contains site-specific information that the server sends to the client along with page output. Cookies can be temporary (with specific expiration times and dates) or persistent.

To store Cookies

// Use this line when you want to save a cookie

Response.Cookies["MyCookieName"].Value = "MyCookieValue";

// How long will cookie exist on client hard disk

Response.Cookies["MyCookieName"].Expires = DateTime.Now.AddDays(1);

To get cookies

if (Request.Cookies["MyCookieName"] != null)

MyCookieValue = Request.Cookies["MyCookieName"].Value;

Server-Based State Management Options

  • Application state : ASP.NET allows you to save values using application state — which is an instance of the HttpApplicationState class — for each active Web application. Application state is a global storage mechanism that is accessible from all pages in the Web application. Thus, application state is useful for storing information that needs to be maintained between server round trips and between requests for pages.


Application["WelcomeMessage"] = "Welcome to test site.";

  • Session state: ASP.NET allows you to save values by using session state — which is an instance of the HttpSessionState class — for each active Web-application session.Session state is similar to application state, except that it is scoped to the current browser session.

Session["FirstName"] = FirstNameTxtBox.Text;

  • Profile Properties: ASP.NET provides a feature called profile properties, which allows you to store user-specific data. This feature is similar to session state, except that the profile data is not lost when a user's session expires. To learn more, click here

6. What is smart navigation in .net?

The cursor position is maintained when the page gets refreshed due to the server side validation and the page gets refreshed.

7. How do I create pages in ASP.Net for mobile devices?

ASP.NET will automatically detect the type of browser making the request. This information is used by the page and by individual controls to render appropriate markup for that browser. You therefore do not need to use a special set of pages or controls for mobile devices. Whether you can design a single page to work with all types of browsers will depend on the page, on the browsers you want to target, and on your own goals.

8.What is ASP.NET Application Life Cycle Overview?. Or What happens when a user request a page from the browser?

  • User requests an application resource from the Web server. The life cycle of an ASP.NET application starts with a request sent by a browser to the Web server (for ASP.NET applications, typically IIS). ASP.NET is an ISAPI extension under the Web server. When a Web server receives a request, it examines the file-name extension of the requested file, determines which ISAPI extension should handle the request, and then passes the request to the appropriate ISAPI extension. ASP.NET handles file name extensions that have been mapped to it, such as .aspx, .ascx, .ashx, and .asmx.

  • ASP.NET receives the first request for the application. When ASP.NET receives the first request for any resource in an application, a class named ApplicationManager creates an application domain. Application domains provide isolation between applications for global variables and allow each application to be unloaded separately. Within an application domain, an instance of the class named HostingEnvironment is created, which provides access to information about the application such as the name of the folder where the application is stored.There will be only one application domain created for an application and all the clients use the same.

  • ASP.NET core objects are created for each request. After the application domain has been created and the HostingEnvironment object instantiated, ASP.NET creates and initializes core objects such as HttpContext, HttpRequest, and HttpResponse. The HttpContext class contains objects that are specific to the current application request, such as the HttpRequest and HttpResponse objects.

  • An HttpApplication object is assigned to the request. After all core application objects have been initialized, the application is started by creating an instance of the HttpApplication class. If the application has a Global.asax file, ASP.NET instead creates an instance of the Global.asax class that is derived from the HttpApplication class and uses the derived class to represent the application.

9. Explain ASP.NET Page Life Cycle Overview?

  • Page request: The page request occurs before the page life cycle begins. When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or whether a cached version of the page can be sent in response without running the page.

  • Start: 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. Additionally, during the start step, the page's UICulture property is set.

  • Page initialization: During page initialization, controls on the page are available and each control's UniqueID property is set. Any themes are also applied to the page. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.

  • Load: During load, if the current request is a postback (First load is not a post back or response to any client action is a postback) control properties are loaded with information recovered from view state and control state.

  • Validation: During validation, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.
    Postback event handling If the request is a postback, any event handlers are called.

  • Rendering: Before rendering, view state is saved for the page and all controls. During the rendering phase, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream of the page's Response property.

  • Unload: Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and any cleanup is performed.

10. what is the difference between Themes and Cascading Style Sheets?

Themes are similar to cascading style sheets in that both themes and style sheets define a set of common attributes that can be applied to any page. Themes can define many properties of a control or page, not just style properties. For example, using themes, you can specify the graphics for a TreeView control, the template layout of a GridView control, and so on.

  • Themes can include graphics.
  • Themes do not cascade the way style sheets do. By default, any property values defined in a theme referenced by a page's Theme property override the property values declaratively set on a control, unless you explicitly apply the theme using the StyleSheetTheme property. For more information, see the Theme Settings Precedence section above.
  • Only one theme can be applied to each page. You cannot apply multiple themes to a page, unlike style sheets where multiple style sheets can be applied.

To apply a theme to a Web site.

In the application's Web.config file, set the Pages element to the name of the theme, either a global theme or a page theme, as shown in the following example:

<pages theme="ThemeName" />

To apply a theme to an individual pageSet the Theme or StyleSheetTheme attribute of the @ Page directive to the name of the theme to use, as shown in the following example:Theme="ThemeName"

11. What are the different types of Caching in ASP.Net?

The main difference between the Cache and Application objects is that the Cache object provides cache-specific features, such as dependencies and expiration policies.


Different types of caching using cache object of ASP.NET

  • Page Output Caching: Page output caching adds the response of page to cache object. Later when page is requested page is displayed from cache rather than creating the page object and displaying it. Page output caching is good if the site is fairly static.
    Page Output caching is easy to implement. By simply using the @OuputCache page directive, ASP.NET Web pages can take advantage of this powerful technique. The syntax looks like this:
    <%@OutputCache Duration="60" VaryByParam="none" %>

  • Page Fragment Caching: If parts of the page are changing, you can wrap the static sections as user controls and cache the user controls using page fragment caching.


12.What are the various modes of storing ASP.NET session?

  • InProc: In this mode Session state is stored in the memory space of the Aspnet_wp.exe process. This is the default setting. If the IIS reboots or web application restarts then session state is lost.

  • StateServer: In this mode Session state is serialized and stored in a separate process (Aspnet_state.exe); therefore, the state can be stored on a separate computer (a state server).

  • SQL SERVER: In this mode Session state is serialized and stored in a SQL Server database.

Session state can be specified in sessionState element of application configuration file. Using State Server and SQL SERVER session state can be shared across web farms but note this comes at speed cost as ASP.NET needs to serialize and deserialize data over network again and again.
Session_End event occurs only in “Inproc mode”. ”State Server” and “SQL SERVER” do not have Session_End event.

13. What is the difference between Absolute and Sliding expiration in Cache?

Absolute Expiration allows you to specify the duration of the cache, starting from the time the cache is activated. The following example shows that the cache has a cache dependency specified, as well as an expiration time of one minute.

Cache.Insert("key","value",dependencies,DateTime.Now.AddMinutes(1),null)

Sliding expiration: The following code specifies that the cache will have a sliding duration of one minute. If a request is made 59 seconds after the cache is accessed, the validity of the cache would be reset to another minute:

Cache.Insert("key","value",dependencies,DateTime.Now.AddMinutes(1), TimeSpan.FromMinutes(1))

14. Compare Datagrid, Datalist and repeater?

A Datagrid, Datalist and Repeater are all ASP.NET data Web controls. They have many things in common like DataSource Property, DataBind Method ItemDataBound and ItemCreated
Itemtemplate can be used for design. Datagrid has a in-built support for Sort, Filter and paging the Data, which is not possible when using a DataList and for a Repeater Control we would require to write an explicit code to do paging.Repeater is fastest followed by Datalist and finally datagrid.

15. What are WebFarm and WebGarden Differences?

Web farms are used to have some redundancy to minimize failures. It consists of two or more web server of the same configuration and they stream the same kind of contents. When any request comes there is switching / routing logic(Load Balancer) which decides which web server from the farm handles the request. For instance we have two servers “Server1” and “Server2” which have the same configuration and content. So there is a special switch which stands in between these two servers and the users and routes the request accordingly.

The routing logic can be a number of different options:-

  • Round-robin: Each node gets a request sent to it “in turn”. So, server1 gets a request, then server2 again, then server1, then server2 again.
  • Least Active: Whichever node show to have the lowest number of current connects gets new connects sent to it. This is good to help keep the load balanced between the server nodes
  • Fastest Reply: Whichever node replies faster is the one that gets new requests. This is also a good option - especially if there are nodes that might not be “equal” in performance.

Web Garden: All requests to IIS are routed to “aspnet_wp.exe” for IIS 5.0 and “w3wp.exe” for IIS 6.0. In normal case i.e. with out web garden we have one worker process instance (“aspnet_wp.exe” / “w3wp.exe”) across all requests. This one instance of worker process uses the CPU processor as directed by the operating system. But when we enable web garden for a web server it creates different instances of the worker process and each of these worker process runs on different CPU. In short we can define a model in which multiple processes run on multiple CPUs in a single server machine are known as a Web garden. To configure Web Garden for asp.net application in web config

<processModel enable ="true" webGarden="true" cpuMask="12" />

cpuMask Specifies which processors on a multiprocessor server are eligible to run ASP.NET processes. For example, if you want to use the first two processors for ASP.NET of a four-processor computer, type 1100. then convert to binary to decimal.