In this article, learn about the top asp.net interview questions and answers which can help you crack the interviews effortlessly.
- What is ASP.NET?
- What is the use of Response.Output.Write()?
This method is used to write the formatted output.
- In which stage of page cycle is the ViewState available?
After the Init() stage and before the Page_Load() state.
- Explain the difference between Server.Transfer and Response.Redirect?
In Server.Transfer processing transfers from one page to the other page without making a round-trip back to the client’s browser, providing a faster response with a little less overhead on the server. The user URL history is not updated in the case of Server.Transfer.
In Response.Redirect processing transfers from one page to the other page with a trip back to the client’s browser. The user URL history is updated in the case of Response.Redirect.
- Name the different validators in ASP.NET.
- Required field Validator
- Range Validator
- Compare Validator
- Custom Validator
- Regular expression Validator
- Summary Validator
- Which validator control is used to make sure the values in two different controls matched?
Compare Validator control.
- Name the base class where all Web Forms are inherited.
- Define ViewState.
ViewState maintains the state of server-side objects between page postbacks.
- After the page postback, where the ViewState is stored?
ViewState is always stored in the hidden field on the page at the client-side and is transported to the client and then again back to the server. It is not stored on the server or any other external source.
- How long the items available in ViewState exists?
It exists for the life of the current page.
- Name and explain the different Session state management options available in ASP.NET.
In-Process Session state management stores the session in memory on the webserver.
Out-of-Process Session state management stores the data in an external server (SQL Server or a State Server). All objects that are stored in the session are required to be serializable interface for Out-of-Process state management.
- How to add an event handler?
With the help of Attributes property of server-side control.e.g.
- Define caching.
Caching is used to increase performance by keeping frequently accessed data or files in memory. The cached file/data can be requested and accessed from the cache instead of that file’s actual location.
- Name the types of caching.
- Output Caching
- Fragment Caching
- Data Caching.
- Which caching will be used if you want to cache the portion of a page instead of the whole page?
Fragment Caching: It will cache the portion of the page generated by request. User controls can be created with the below code:
<%@ OutputCache Duration="120" VaryByParam="CategoryID;SelectedID"%>
- Name the events in the page life cycle.
- Can we run a web application without the web.Config file?
- Can we create a web application with both webforms and MVC?
Yes, by including below MVC assembly references in the web forms application to create a hybrid application.
- Can code files of different languages be added in the App_Code folder?
No, the code files must be in the same language.
- Define the Protected Configuration.
Protected Configuration is a feature that is used to secure connection string information.
- Write a code to send an email from an ASP.NET application.
MailMessage mailMess = new MailMessage (); mailMess.From = "email@example.com"; mailMess.To = "firstname.lastname@example.org"; mailMess.Subject = "Test email"; mailMess.Body = "Hi This is a test mail."; SmtpMail.SmtpServer = "localhost"; SmtpMail.Send (mailMess);
Here, MailMessage and SmtpMail are classes defined System.Web.Mail namespace.
- How to prevent browsers from caching an ASPX page?
By using SetNoStore on HttpCachePolicy object exposed by the Response object’s Cache property:
Response.Write (DateTime.Now.ToLongTimeString ());
- What is the good practice for implementing validations on the aspx page?
The best way for validating data of a web page is Client-side validation that reduces the network traffic and saves server resources. It is also good to have Server-side validation because sometimes the browser bypasses the client-side validations.
- Name the event handlers available in the Global.asax file.
Application Events including Application_Start , Application_End, Application_AcquireRequestState, Application_AuthenticateRequest, Application_AuthorizeRequest, Application_BeginRequest, Application_Disposed, Application_EndRequest, Application_Error, Application_PreRequestHandlerExecute,Application_PreSendRequestContent, Application_PreSendRequestHeaders, Application_ReleaseRequestState, Application_ResolveRequestCache, Application_UpdateRequestCache
Session Events including Session_Start, Session_End
- Name the protocol used to call a Web service.
- Are multiple web config files available for an asp.net application?
- Explain the difference between the web config file and the machine config file.
The Web config file is specific only to a web application, whereas the machine config file is specific to a machine or server. Multiple web config files can be there into an application, whereas only one machine config file is there on a server.
- What is role-based security?
Role-Based Security is used to implement security based on roles assigned to user groups in the organization. We can allow or deny the users based on their role in the organization. Several built-in groups that include Administrators, Users and Guests are available in Windows.
<AUTHORIZATION>< authorization >
< allow roles=”Domain_Name\Administrators” / > < !– Allow Administrators in domain. — >
< deny users=”*” / > < !– Deny anyone else. — >
< /authorization >
- Define Cross Page Posting.
After clicking on the submit button on the web page, the page post the data to the same page. The technique in which the data is posted to the different pages is called Cross Page Posting. This is achieved by setting the POSTBACKURL property of the button that causes the postback.
- How to apply Themes to an asp.net application?
Theme can be specified in web.config file with the help of below code:
<configuration> <system.web> <pages theme="Windows7" /> </system.web> </configuration>
- What is RedirectPermanent?
It performs a permanent redirection from the requested URL to the specified URL. Once the redirection is complete, it returns 301 Moved Permanently responses.
- Explain MVC.
MVC (Model-View-Controller) is a framework to create web applications. It separates the application logic from UI, and the input and events from the user will be controlled by the Controller.
- How does passport authentication work?
It checks for the passport authentication cookie, and if the cookie is not available, then the application redirects the user to the Passport Sign-in page. It then authenticates the user details on the sign-in page and, if valid, then stores the authenticated cookie on the client machine and redirects the user to the requested page.
- Explain the advantages of Passport authentication.
All the websites are using single login credentials. So no need to remember the login credentials. Users can also maintain their information in a single location.
- Explain the asp.net Security Controls.
- <asp:Login>: It provides a standard login functionality that allows the users to enter their credentials
- <asp:LoginName>: It allows to display the name of the logged-in user
- <asp:LoginStatus>: It checks and displays whether the user is authenticated or not
- <asp:LoginView>: It provides various login views depending on the selected template
- <asp:PasswordRecovery>: It emails the users their lost password
By using <CONTROL -name>Attribtues.Add(scriptname,scripttext) method.
- Name the event where the controls fully loaded.
Page load event.
- Explain boxing and unboxing.
Boxing means assigning a value type to reference type variable.
Unboxing means reversing of boxing, i.e., assigning reference type variable to value type variable.
- What is the difference between the strong typing and weak typing?
In a strong typing method, the data types of variables are checked at compile time. But in the case of a weak typing method, the data types of variables are checked at runtime.
- How to force all the validation controls to run?
The Page.Validate() method helps to force all the validation controls to run and to perform validation.
- Name all templates of the Repeater control.
- Name the major built-in objects.
- Name the appSettings Section in the web.config file.
The appSettings are the user-defined values.
<em><configuration> <appSettings> <add key="Connection" value="server=local; pwd=password; database=default" /> </appSettings></em>
- Which data type is supported by the RangeValidator?
Integer, Double, String, Currency, and Date.
- Explain the difference between an HtmlInputCheckBox control and an HtmlInputRadioButton control.
Multiple item selection is possible in HtmlInputCheckBoxcontrol, whereas we can select only a single item from the group of items in HtmlInputRadioButton controls.
- Which namespaces are important to create a localized application?
- Name the different types of cookies.
Non-Persistent Cookie: It persists on the client machine only for a single session until the user does not log out.
Persistent Cookie: It resides on a user’s machine for a period specified for its expiry, such as ten days, one month, and never.
- What is the file extension of the web service?
.asmx is the file extension of web service.
- Name the components of ADO.NET.
Dataset, Data Reader, Data Adaptor, Command, and connection.
- Explain the difference between ExecuteScalar and ExecuteNonQuery.
ExecuteScalar returns output value, but ExecuteNonQuery does not return any value; instead, it returns the number of rows affected by the query. ExecuteScalar is used for fetching a single value, but ExecuteNonQuery is used to execute Insert and Update statements.