Asp.Net Questions & Answers
1)
What
is ASP.Net?
Asp.Net is a
framework developed by Microsoft on which we can develop new generation web
sites using web forms aspx, MVC, HTML, Javascript, CSS etc. Its successor of
Microsoft Active Server Pages(ASP). Currently there is ASP.NET 4.0, which is
used to develop web sites. There are various page extensions provided by
Microsoft that are being used for web site development. Eg: aspx, asmx, ascx,
ashx, cs, vb, html, XML etc.
2)
What
is AutoPostBack and How do you disable AutoPostBack?
As the AutoPostBack can be disabled on an ASP.NET page by disabling
AutoPostBack on all the controls of a page. AutoPostBack is caused by a control
on the page.
3)
What
is ViewState in ASP.NET?
Http is a stateless protocol. Hence
the state of controls is not saved between postbacks. Viewstate is the means of
storing the state of server side controls between postbacks. The information is
stored in HTML hidden fields. In other words, it is a snapshot of the contents
of a page.
You can disable ViewState by
a control by setting the EnableViewState property to false.
4)
In
which event of page cycle is the ViewState
available?
After the Init() and before the Page_Load().
5)
What
is the difference between Response.Redirect
and Server.Transfer?
In Server.Transfer page processing transfers from one page to the other
page without making a round-trip back to the client's browser. This provides a
faster response with a little less overhead on the server. The clients url
history list or current url Server does not update in case of Server.Transfer.
Response.Redirect is used to redirect the user's browser to another page
or site. It performs trip back to the client where the client's browser is
redirected to the new page. The user's browser history list is updated to
reflect the new address.
6)
Explain
the ASP.NET page life cycle in brief.
ASP.NET goes
through a series of stages in the life cycle of each page.
Page request- The user requests a page. ASP.NET decides whether to compile
it or serve it from a cache.
Page Start- The Request and Response objects are created.
Page Initialization- All page controls are initialized, and any themes are applied.
Page Load- ASP.NET uses the view state and control state properties to set
the control properties. Default values are set in the controls.
Postback event handling- This event is triggered if the same page is loaded again.
Rendering- ASP.NET saves the view state for the page and writes the output
of rendering to the output stream. It happens just before the complete web page
is sent to the user.
Unload- The
rendered page gets sent to the client. ASP.NET unloads page properties and
performs cleanup. All unwanted objects are removed from memory.
7)
What
is the use of Response.Output.Write()?
We can write formatted output using Response.Output.Write().
8)
From
which base class all Web Forms are inherited?
Page class.
9)
What
are the different validators in ASP.NET?
·
Required
field Validator
·
Range
Validator
·
Compare
Validator
·
Custom
Validator
·
Regular expression
Validator
·
Summary
Validator
10) Which validator control you use if you need to
make sure the values in two different controls matched?
Compare Validator control.
11) What is View State in ASP.NET?
View State is where
data is used to preserve page values and control values of Web Forms during
postback event handling. Data can be stored as hidden fields on the client web
page.
12) Where the ViewState is stored after the page Postback?
ViewState is
stored in a hidden field on the page at client side. ViewState is transported
to the client and back to the server, and is not stored on the server or any
other external source.
13) How long the items in ViewState exists?
They exist for the life of the current page.
14) What are the different Session state management
options available in ASP.NET?
In-Process - In this Session is stored in Web Server Memory
Out-of-Process - In this Session is stored in external server or system , there are
two ways to maintain it and it is necessary to serialize all objects to use it,
there are two types as below
State
Server - In This, Session is stored on
external server or computer.
Sql
Server - In this , Session is stored in
Server Server .
15) What is caching?
Caching is a technique, in which, we can cache the data for frequently
access, to increase performance or files in memory. The request for a cached
file/data will be accessed from cache instead of actual location of that file.
16) What are the types of Authentication in ASP.NET?
There are three types of authentication available in ASP.NET:
Windows Authentication- This authentication method uses built-in Windows
security features to authenticate a user.
Forms Authentication- Authenticates against a customized list of users
or users in a database.
Passport Authentication- Validates against Microsoft Passport service
which is basically a centralized authentication service.
17) How you can add an event handler?
Using the Attributes property of server side control.
For example-
btnSubmit.Attributes.Add("onMouseOver","JavascriptCode();")
18) Explain , What are the different types of
caching?
ASP.NET has 3 kinds of caching :
·
Output
Caching,
·
Fragment
Caching,
·
Data
Caching.
19) Which type if caching will be used if we want to cache the portion of a page
instead of whole page?
Fragment Caching: It caches the portion of the page generated by the
request. For that, we can create user controls with the below code:
<%@ OutputCache
Duration="120" VaryByParam="CategoryID;SelectedID"%>
20) Can we have a web application running without
web.Config file?
Yes
21) Is it possible to create web application
with both webforms and mvc?
Yes. We have to include below mvc
assembly references in the web forms application to create hybrid application.
System.Web.Mvc
System.Web.Razor
System.ComponentModel.DataAnnotations
22) Can we add code files of different
languages in App_Code folder?
No. The code files must be in same language to be kept in App_code
folder.
23) How can we use e-mail functionality in ASP.NET application?
First add assembly for System.Web.Mail then code as below
MailMessage
mailMess = new MailMessage ();
mailMess.From =
"ajay@gmail.com
";
mailMess.To =
"kamal@gmail.com";
mailMess.Subject
= "Test Email";
mailMess.Body =
"Hello Team This is test mail.";
SmtpMail.SmtpServer
= "localhost";
SmtpMail.Send
(mailMess);
24) What is the difference between web
config and machine config?
Web config file is specific to a web application where as machine config
is specific to a machine or server. There can be multiple web config files into
an application where as we can have only one machine config file on a server.
25) How can we prevent browser from caching
an ASPX page?
We can SetNoStore on HttpCachePolicy object exposed by the Response
object's Cache property:
Response.Cache.SetNoStore
();
Response.Write
(DateTime.Now.ToLongTimeString ());
26) What is the good practice to implement
validations in aspx page?
Client-side validation is the best way to validate data of a web page.
It reduces the network traffic and saves server resources.
27) What is Global.asax file and what are the event handlers that we can have
in Global.asax file?
Global.asax file is
a file that executes first time when application runs. there are many
Application Events inside it as below
Application_Start , Application_End, Application_AcquireRequestState,
Application_AuthenticateRequest, Application_AuthorizeRequest,
Application_BeginRequest, Application_Disposed, Application_EndRequest, Application_Error,
Application_PostRequestHandlerExecute,
Application_PreRequestHandlerExecute,Application_PreSendRequestContent,
Application_PreSendRequestHeaders, Application_ReleaseRequestState,
Application_ResolveRequestCache, Application_UpdateRequestCache
Session Events: Session_Start,Session_End
28) Which protocol is used to call a Web service?
HTTP Protocol
29) Explain role based security ?
Role Based Security used to implement security based on roles assigned
to user groups in the organization.
Then we can allow or deny users based on their role in the organization.
Windows defines several built-in groups, including Administrators, Users, and
Guests.
<AUTHORIZATION><
authorization >
< allow
roles="Domain_Name\Administrators" / > < !-- Allow Administrators in domain. --
>
< deny
users="*" / > < !-- Deny
anyone else. -- >
<
/authorization >
30) What is Cross Page Posting?
When we click submit button on a web page, the page post the data to the
same page. The technique in which we post the data to different pages is called
Cross Page posting. This can be achieved by setting POSTBACKURL property of the
button that causes the postback. Findcontrol method of PreviousPage can be used
to get the posted values on the page to which the page has been posted.
31) How can we apply Themes to an asp.net
application?
We can specify the theme in web.config file. Below is the code example
to apply theme:
<configuration>
<system.web>
<pages theme="Windows7" />
</system.web>
</configuration>
32) What is RedirectPermanent in ASP.Net?
RedirectPermanent Performs a permanent redirection from the requested
URL to the specified URL. Once the redirection is done, it also returns 301
Moved Permanently responses.
33) In which event are the controls fully
loaded?
Page load event.
34) what is boxing and unboxing?
Boxing is assigning a value type to reference type variable.
Unboxing is reverse of boxing ie. Assigning reference type variable to
value type variable.
35) Differentiate strong typing and weak
typing
In strong typing, the data types of variable are checked at compile
time. On the other hand, in case of weak typing the variable data types are
checked at runtime. In case of strong typing, there is no chance of compilation
error. Scripts use weak typing and hence issues arises at runtime.
36) How we can force all the validation
controls to run?
The Page.Validate() method is used to force all the validation controls
to run and to perform validation.
37) What is the appSettings Section in the
web.config file?
The appSettings block in web config file sets the user-defined
values for the whole application.
For example, in the following code snippet, the specified
ConnectionString section is used throughout the project for database
connection:
configuration>
<appSettings>
<add
key="ConnectionString" value="server=local; pwd=passwd;
database=default" />
</appSettings>
38) Which data type does the RangeValidator
control support?
The data types supported by the RangeValidator control are Integer,
Double, String, Currency, and Date.
39) Explain the difference between an
HtmlInputCheckBox control and an HtmlInputRadioButton control?
In HtmlInputCheckBoxcontrol, multiple item selection is possible whereas
in HtmlInputRadioButton controls, we can select only single item from the group
of items.
40) Which namespaces are necessary to create
a localized application?
·
System.Globalization
·
System.Resources
41) What are the different types of cookies
in ASP.NET?
Session Cookie - Resides on the client machine for a single session until the user does
not log out.
Persistent Cookie - Resides on a user's machine for a period specified for its expiry,
such as 10 days, one month, and never.
42) What are the components of ADO.NET?
The components of ADO.Net are Dataset, Data Reader,
Data Adaptor, Command, connection.
43) Explain the difference between ExecuteScalar and ExecuteNonQuery?
ExecuteScalar returns
output value where as ExecuteNonQuery does not return any
value but the number of rows affected by the query. ExecuteScalar used for
fetching a single value and ExecuteNonQuery used to execute
Insert and Update statements.
44) What is the file extension of web
service?
Web services have file extension .asmx..
45) What is XHTML?
eXtensible HyperText Markup Language is part of the family
of XML markup languages. It mirrors or extends versions of the widely used
HyperText Markup Language, the language in which Web pages are formulated. It
is a stricter and cleaner version of HTML.
46) Can I deploy the application without
deploying the source code on the server?
The publish web site command lets us to precompile an
asp.net application and copy the precompiled assemblies to a target server.
This is the easiest way to use the precompiled deployment feature. This
precompilation process builds each page in your
web application into a single application DLL and some placeholder files. These
files can then be deployed to the server.
47) Does ViewState affect performance? What
is the ideal size of a ViewState? How
can you compress a viewstate?
Viewstate
stores the state of controls in HTML hidden fields. At times, this information
can grow in size. This does affect the overall responsiveness of the page,
thereby affecting performance. The ideal size of a viewstate should be not more
than 25-30% of the page size.
Viewstate can be compressed to
almost 50% of its size. .NET also provides the GZipStream orDeflateStream to
compress viewstate.
48) How can you detect if a viewstate has been tampered?
By setting the EnableViewStateMac to true in the @Page
directive. This attribute checks the encoded and encrypted viewstate for
tampering.
49) Can I use different programming
languages in the same application?
Yes. Each page can be written with a different programming language in
the same application. You can create a few pages in C# and a few in VB.NET.
50) Can the App_Code folder contain source
code files in different programming languages?
No. All source code files kept in the root App_Code folder must be in
the same programming language.
Update: However, you can create two subfolders inside the App_Code and
then add both C# and VB.NET in the respective
subfolders. You also have to add configuration settings in the web.config
for this to work.
51) How do you secure your connection string
information?
By using the Protected Configuration feature.
52) How do you secure your configuration
files to be accessed remotely by unauthorized users?
ASP.NET configures
IIS to deny access to any user that requests access to the Machine.config or
Web.config files.
53) How can I configure ASP.NET applications that are running on a remote machine?
You can use the Web Site Administration Tool to configure remote websites.
54) How many web.config files can I have in
an application?
You can keep multiple web.config files in an application. You can place
a Web.config file inside a folder or wherever you need (apart from some
exceptions) to override the configuration settings that are inherited from a
configuration file located at a higher level in the hierarchy.
55) I have created a configuration setting
in my web.config and have kept it at the root level. How do I prevent it from
being overridden by another web.config that appears lower in the hierarchy?
By setting the element's Override attribute to false.
56) What is the difference between
Response.Write and Response.Output.Write?
As quoted by Scott Hanselman, the short answer is that the latter gives
you String.Format-style output and the former doesn't.
57) What is Cross Page Posting? How is it
done?
By default, ASP.NET submits a form to the same page. In cross-page
posting, the form is submitted to a different page. This is done by setting the
‘PostBackUrl’ property of the button(that causes postback) to the desired page.
In the code-behind of the page to which the form has been posted, use the
‘FindControl’method of the ‘PreviousPage’ property to reference the data
of the control in the first page.
58) Can you change a Master Page dynamically
at runtime? How?
Yes. To change a master page, set the MasterPageFile property to point
to the .master page during the PreInit page event.
59) How do you apply Themes to an entire
application?
By specifying the theme in the web.config file.
<configuration>
<system.web>
<pages
theme=”BlueMoon” />
</system.web>
</configuration>
|
|
60) How do you exclude an ASP.NET page from using Themes?
To remove themes from your page, use the EnableTheming attribute of the
Page directive.
61) Your client complains that he has a
large form that collects user input. He wants to break the form into sections,
keeping the information in the forms related. Which control will you use?
The ASP.NET Wizard Control.
62) Do webservices support data reader?
No. However it does support a dataset.
63) What is use of the AutoEventWireup
attribute in the Page directive ?
The AutoEventWireUp is a boolean attribute that allows automatic wireup
of page events when this attribute is set to true on the page. It is set to
True by default for a C# web form whereas it is set as False for VB.NET forms.
Pages developed with Visual Studio .NET have this attribute set to false, and
page events are individually tied to handlers.
64) What happens when you change the
web.config file at run time?
ASP.NET invalidates
the existing cache and assembles a new cache. Then ASP.NET automatically
restarts the application to apply the changes.
65) Can you programmatically access IIS
configuration settings?
Yes. You can use ADSI, WMI, or COM interfaces to configure IIS programmatically.
66) How does Application Pools work in IIS
6.0?
As explained under the IIS documentation, when you run IIS 6.0 in
worker process isolation mode, you can separate different Web applications and
Web sites into groups known as application pools. An application
pool is a group of one or more URLs that are served by a worker
process or set of worker processes. Any Web directory or virtual directory can
be assigned to an application pool.
Every application within an application pool shares the same worker
process. Because each worker process operates as a separate instance of the
worker process executable, W3wp.exe, the worker process that services one
application pool is separated from the worker process that services another.
Each separate worker process provides a process boundary so that when an
application is assigned to one application pool, problems in other application
pools do not affect the application. This ensures that if a worker process
fails, it does not affect the applications running in other application pools.
Use multiple application pools when you want to help ensure that
applications and Web sites are confidential and secure. For example, an
enterprise organization might place its human resources Web site and its
finance Web site on the same server, but in different application pools.
Likewise, an ISP that hosts Web sites and applications for competing companies
might run each company’s Web services on the same server, but in different
application pools. Using different application pools to isolate applications
helps prevent one customer from accessing, changing, or using confidential
information from another customers site.
In HTTP.sys, an application pool is represented by a request
queue, from which the user-mode worker processes that service an
application pool collect the requests. Each pool can manage requests for one or
more unique Web applications, which you assign to the application pool based on
their URLs. Application pools, then, are essentially worker process configurations
that service groups of namespaces.
Multiple application pools can operate at the same time. An application,
as defined by its URL, can only be served by one application pool at any time.
While one application pool is servicing a request, you cannot route the request
to another application pool. However, you can assign applications to another
application pool while the server is running.
Note: This ASP.NET Questions and Answers for
Experienced Developers have been taken from forums, my colleagues and my own
experience of conducting interviews. I have tried to mention the contributor
wherever possible. If you would like to contribute.