Top 50 ASP.Net Interview Questions and Answers
1. What is ASP.NET?
ASP.NET is a web development platform developed by Microsoft. It allows developers to build dynamic web applications, web services, and websites using HTML, CSS, JavaScript, and server-side scripts. It is a part of the .NET framework.
2. What is the difference between ASP and ASP.NET?
ASP is an older server-side script engine for dynamically generated web pages, also known as Active Server Pages. ASP.NET is a web application framework developed and marketed by Microsoft to allow programmers to build dynamic web sites, applications, and services. ASP.NET offers better language support, a large set of user controls, XML-based components, and better user authentication.
3. Explain the difference between web.config and machine.config
Web.config files apply settings to each web application, while machine.config applies settings to all ASP.NET applications on a machine. Every web application has a web.config file, while a machine has only one machine.config file, which is at the root of the .NET Framework.
4. What is ViewState in ASP.NET?
ViewState is a feature in ASP.NET to persist the values of the page and its controls between round trips. It’s a hidden field on the page, encoded in Base64. ViewState is used mainly for page-specific data, whereas session data is specific to a user across the entire application.
5. Can you explain the Page lifecycle of an ASP.NET MVC page?
The stages are as follows:
- Page request
- Start
- Page initialization
- Load
- Postback event handling
- Rendering
- Unload
6. What is the code-behind feature in ASP.NET?
The code-behind feature in ASP.NET allows separation of the business logic of a web page from its design. This allows for a cleaner and more organized coding structure.
7. How do we implement authentication in ASP.Net?
ASP.NET provides Forms Authentication, Windows Authentication, and Passport Authentication.
8. What are HTTP Handlers and HTTP Modules?
HTTP Handlers are the end-points in ASP.NET Web Server used to process the request. HTTP Modules are classes that inspect or modify requests as they come in and responses as they go out.
9. What is ASP.NET MVC?
ASP.NET MVC is a web application framework developed by Microsoft which implements the model–view–controller (MVC) pattern.
10. What are the three segments of the MVC pattern?
The three segments are Model, View, and Controller. The Model represents the data and the business logic. The View displays the data to the user. The Controller handles the user interactions.
11. Can you explain the concept of routing in ASP.NET MVC?
Routing is a pattern matching system that handles the browser requests and matches them with the routes defined in the Route table. A programmer can define a URL structure and map it to a controller.
12. What is the use of the ViewBag in ASP.NET MVC?
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0. Basically, it is a wrapper around ViewData and also used to pass data from the controller to the view.
13. What is Razor in ASP.NET MVC?
Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages.
14. What are Layouts in ASP.NET MVC?
Layouts are like MasterPages in ASP.NET Web Forms. They allow you to define a common template for your site and then fill differing content into placeholders on the template.
15. What is the difference between TempData, ViewData, and ViewBag?
TempData is used to pass data from the current request to the next request. ViewData and ViewBag are used to pass data from controllers to views. The difference between ViewData and ViewBag is that ViewBag uses the dynamic feature that was added in C# 4.0.
16. What is AJAX in ASP.NET?
AJAX stands for Asynchronous JavaScript and XML. It’s a set of web development techniques using many web technologies on the client side to create asynchronous web applications.
17. What are the benefits of using AJAX in an ASP.NET web application?
AJAX enables a smoother, more responsive web experience. It allows for the asynchronous processing of web pages, which means that a part of the web page can be updated without having to reload the entire page.
18. What is Entity Framework in ASP.NET?
Entity Framework (EF) is an open-source object-relational mapping (ORM) framework for .NET. It enables developers to work with data in the form of domain-specific objects and properties.
19. What is LINQ in ASP.NET?
LINQ stands for Language Integrated Query, which allows you to write queries in .NET in a more intuitive way than by constructing strings. It provides a way to query different data sources like databases, XML documents, in-memory objects etc.
20. What is a Session in ASP.NET?
A Session is a server-side storage mechanism that allows you to persist data across multiple user requests. Each session is associated with a unique session ID.
21. Can you explain the different types of state management techniques in ASP.NET?
There are two types of state management techniques: Client-side (Viewstate, Cookies, Query strings) and Server-side (Application state, Session state, and database).
22. What are Filters in ASP.NET MVC?
Filters are used to inject extra logic at the different levels of MVC Framework request processing. These are custom classes where you can write custom logic to execute before or after an action method executes.
23. What is a Partial View in ASP.NET MVC?
Partial Views in MVC are used for rendering a portion of view content. It is reusable and can be embedded inside another view.
24. What is the App_Data folder in ASP.NET?
The App_Data folder is used to store file-based data like .mdf (SQL Server) files, XML files, etc. It is a place to keep a database. It is used in local-based applications.
25. What is the Global.asax file used for in ASP.NET?
Global.asax, also known as the ASP.NET application file, is used to handle application and session-level events. You can write code inside this file to respond to application-level events, such as Application_Start, Application_End, Session_Start, etc.
26. How can we do exception handling in ASP.NET MVC?
In ASP.NET MVC, we can handle exceptions in multiple ways:
- Override the “OnException” method.
- Use the “HandleError” attribute at the controller level.
- Use the “Application_Error” method in the Global.asax file.
27. What are Scaffolding templates in ASP.NET MVC?
Scaffolding in ASP.NET MVC is a code generation framework for ASP.NET Web applications. Visual Studio includes pre-installed code generators for MVC and Web API projects.
28. What are Action Filters in ASP.NET MVC?
Action Filters are attributes that can be applied to an Action Method or a Controller. They add pre-action and post-action behavior to the controller action methods.
29. What is Bundling and Minification in ASP.NET MVC?
Bundling helps us to bundle multiple JavaScript and CSS files into a single entity thus reducing multiple requests to server. Minification helps to minimize the size of JavaScript and CSS files, reducing the loading time andimproving the performance of the web page.
30. What is a ContentResult in ASP.NET MVC?
ContentResult is an action result which represents a text result. It encapsulates a string that is used to generate an HTTP response.
31. What are Web API’s in ASP.NET?
ASP.NET Web API is a framework for building HTTP services that can be consumed by a broad range of clients including browsers, mobiles, iPhone and tablets. It is very similar to ASP.NET MVC since it contains the MVC features.
32. What is the use of Keep and Peek in TempData in ASP.NET MVC?
“Keep” and “Peek” are methods to persist data in TempData. Keep method helps to persist the TempData when it reads. Peek method is used to read the TempData value without making it null.
33. What is a Web Garden in ASP.NET?
A web garden allows multiple processes to run on the same machine. Each process will have its own memory space, which can improve application’s performance if used properly.
34. What is a Web Farm in ASP.NET?
Web Farm is a multi-server scenario. So in a web farm, the application is hosted on multiple servers to distribute the load.
35. How can we perform validations in ASP.NET MVC?
In ASP.NET MVC, validation is done using data annotations. Data annotations are nothing but attributes that you can apply on model properties. For example, Required, StringLength, Range etc.
36. Can we add constraints to the route? If yes, how?
Yes, we can add constraints to the route in the route template itself. It can be done by defining a constraint inline in the route template after the parameter name, separated by a colon.
37. What is Dependency Injection in ASP.NET MVC?
Dependency Injection (DI) is a design pattern that allows us to eliminate the hard-coded dependencies and make our applications loosely coupled, extendable and maintainable. ASP.NET MVC framework supports DI that allows injecting objects into a class, instead of relying on the class to create the object itself.
38. What are HTML helpers in ASP.NET MVC?
HTML helpers are methods that return HTML string. They help in rendering HTML controls in the view. They generate HTML output that is sent to the client.
39. What is Cross-Site Scripting (XSS)? How does ASP.NET MVC prevent it?
Cross-Site Scripting (XSS) attacks are a type of injection, in which malicious scripts are injected into otherwise benign and trusted websites. ASP.NET MVC uses anti-XSS techniques by encoding the output of Html Helpers to ensure a secure platform.
40. What is Cross-Site Request Forgery (CSRF)? How does ASP.NET MVC prevent it?
Cross-Site Request Forgery (CSRF) is an attack that tricks the victim into submitting a malicious request. ASP.NET MVC prevents CSRF attacks by using AntiForgeryToken().
41. What is the role of the ActionResult class in ASP.NET MVC?
The ActionResult class in ASP.NET MVC is an abstract class that serves as the base class for all action result types.
42. How can we handle Errors in ASP.NET MVC?
Errors can be handled in ASP.NET MVC at multiple levels:
- At action level using HandleError attribute.
- At controller level using OnException method.
- At global level using Application_Error event in the Global.asax file.
43. What is Caching in ASP.NET?
Caching is a technique used to store frequently used data or files in a cache to improve performance. ASP.NET has three types of caching: Output Caching, Data Caching, and Fragment Caching.
44. What are the different types of Caching in ASP.NET?
ASP.NET supports three types of caching:
- Output Caching: Caches the whole response of a page or controller action.
- Data Caching (or Application Caching): Caches arbitrary objects.
- Fragment Caching (or Partial Page Caching): Caches portions of a response, similar to Output caching.
45. What is the use of the Default Route {controller}/{action}/{id}?
The default route maps incoming URLs to the controller and action method. Here, “controller” refers to the name of the controller, “action” refers to the action method within the controller, and “id” is used to pass extra parameters to the action method.
46. What is the role of the IIS in ASP.NET?
IIS (Internet Information Services) is a web server from Microsoft that runs on Windows systems and is used to host ASP.NET web applications. It provides a secure, manageable, modular and extensible platform for reliably hosting websites, services, and applications.
47. How can you secure an ASP.NET Web API?
Securing an ASP.NET Web API can be done in several ways including using token-based authentication, OAuth, JWT (JSON Web Token) or using API keys.
48. What is the use of the RenderBody and RenderPage in ASP.NET MVC?
RenderBody is a placeholder where all the view-specific pages you will be loading. RenderPage is used to render a specified page at the specified location in a layout.
49. What is the difference between TempData and Session in ASP.NET MVC?
TempData and Session are both used to store temporary data, but TempData is used to pass data from the current request to the next request, whereas Session data is used to store data between multiple requests from the same user.
50. What are asynchronous controllers in ASP.NET MVC?
Asynchronous controllers are used to perform long-running operations asynchronously so that the main thread is not blocked. This prevents the web application from becoming unresponsive. It helps in achieving more scalability, and makes better use of resources.