Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What is a postback?

Answer»

A postback will occur when clients MAKE a request for a server response from their viewing page. Consider a simple example of a form completion by the user and hitting the “submit” button. In this case, the property IsPostBack will be TRUE, so the request will be processed by the server and a response will be sent back to the client.

A postback helps to REFRESH the entire page when a data retrieval request has been MADE with a callback WITHOUT completely refreshing the page.

2.

Explain different development approaches with the Entity framework.

Answer»

Three different development approaches will be provided by the Entity framework while dealing with the database and data access layer in the application according to the NEED of the project. They are:

Database-First APPROACH: This approach will be used if there is an already existing database schema. Here, context and entities will be generated for the existing database using the EDM(Entity Data Model) wizard. This approach is considered to be the best for applications that will make USE of an already existing database.

The Database-First approach is useful in the following cases:

  • While working with a legacy database.
  • While working on an application that is data-centric.
  • In scenarios such as the database design is carried out by another team and the development of the application begins only when the database is ready.

Code-First Approach: This approach is used when the existing database is not present for your application. In this approach, you will write the entities(domain classes) and context classes, then the database will be created from these classes with the help of migration commands.

This approach is considered to be the best for highly domain-centric applications and that will create the domain model classes in the beginning. Here, the database will be required only as a persistence mechanism for these newly created domain models. That implies, if a developer is following the principles of Domain-Driven Design(DDD), they will start the coding with the creation of domain classes and then the database required will be generated for persisting their data.

The Code First approach is useful in the following situations:

  • When there is an absence of logic in the database
  • When there is no context code and auto-generated model
  • If there is no MANUAL modification of the database

Model-First Approach: In this approach, the entities, inheritance hierarchies, and relationships will be created directly on the visual DESIGNER and then can generate context class, database script, and entities using the visual model.

The Model-First approach is useful in the situation:

  • When we want to make use of the Visual Entity Designer.
3.

What is the purpose of ”beforeRender()”, “beforeFilter()”, and “afterFilter()” functions in Controller?

Answer»
  • beforeFilter(): This function will be executed before each action in the controller. It will inspect user PERMISSIONS or check for an active session.
  • beforeRender(): This function will be executed before the RENDERING of the view and immediately after controller action logic. This function will not be frequently used, but it might be needed if you MANUALLY call the render() function before the end of a GIVEN action.
  • afterFilter(): This function will be called after each controller action, and also after the completion of rendering. It is the last METHOD in the controller to run.
4.

Discuss a few necessary namespaces used in ASP.NET MVC.

Answer»

Few significant namespaces that are used in ASP.NET MVC applications are:

  • System.WEB.Mvc: It includes classes and interfaces that support the MVC pattern for Web applications in ASP.NET. It also has classes that represent controller factories, views, partial views, controllers, model binders, and ACTION results.
  • System.Web.Mvc.AJAX: It consists of classes that support Ajax scripting within an application in ASP.NET MVC.
  • System.Web.Mvc.Html: It consists of classes that help to RENDER HTML controls in an ASP.NET MVC application. Also, it has classes that will support partial views, validation, forms, LINKS, and input controls.
5.

How to use multiple models to a single view in ASP.NET MVC?

Answer»

ASP.NET MVC will PERMIT only a single model to be bound to each view, but there are many workarounds for this. For example, the class ExpandoObject in the ASP.NET framework is a dynamic model which will be passed to a view.

Another common solution is the usage of ViewModel class(a class with SEVERAL models), ViewBag(a property of the ControllerBase class which is dynamic), or ViewData(a dictionary object). It is also possible to use Tuple, JSON, and RenderAction.

In the example given below, you can get to know about how to use multiple models in a single view with the help of the ViewModel class. Here, ViewModel has two properties namely Managers and Employees that acts as a model.

ViewModel class:

public class ViewModel{ public IEnumerable<Manager> Managers { get; set; } public IEnumerable<Employee> Employees { get; set; }}

Controller CODE:

public ActionResult IndexViewModel(){ ViewBag.Message = "Welcome to my example program!"; ViewModel demomodel = new ViewModel(); demomodel.Managers = GetManagers(); demomodel.Employees = GetEmployees(); return View(demomodel);}

View code:

@using MultipleModelInOneView;@model ViewModel@{ ViewBag.Title = "Home PAGE";}<h2>@ViewBag.Message</h2><p><b>Manager List</b></p><table> <tr> <th>ManagerId</th> <th>Department</th> <th>Name</th> </tr> @foreach (Manager manager in Model.Managers) { <tr> <td>@manager.ManagerId</td> <td>@manager.Department</td> <td>@manager.Name</td> </tr> }</table><p><b>Employee List</b></p><table> <tr> <th>EMPLOYEEID</th> <th>Name</th> <th>Designation</th> </tr> @foreach (Employee employee in Model.Employees) { <tr> <td>@employee.EmployeeId</td> <td>@employee.Name</td> <td>@employee.Designation</td> </tr> }</table>
6.

What is a glimpse in ASP.NET MVC?

Answer»

Glimpse is a WEB debugging and diagnostics TOOL that can be used with your ASP.NET or ASP.NET MVC applications for finding the information related to performance, debugging, and diagnostic. It helps you to obtain information regarding model BINDING, timelines, environment, routes, ETC. It is possible to integrate Glimpse with insights into an application.

An intuitive user interface will be provided by Glimpse for examining the performance data of an application. Glimpse adds a widget to the web pages for VIEWING the performance-related data as you browse through the application web pages.

7.

What is the use of Validation Summary?

Answer»
  • The Validation Summary helper method is USEFUL in unordered list(UL element) generation that belongs to validation messages present in the object of MODEL State Dictionary.
  • It can be used for displaying all error messages RELATED to all the fields. Also, it can be used for displaying custom error messages.
  • ValidationSummary will not only display the first MESSAGE for any property instead it displays all error messages on the page. If you pass the first parameter value as true to ValidationSummary, only the messages having String.Empty as their name will be displayed.

For example, the below-given code will display a heading “Please fix the problem:” for a list of all the String.Empty messages. @Html.ValidationSummary(True, "Please fix the problem:")

8.

What are HTML Helper methods?

Answer»

Helper methods are useful in rendering the HTML in the view. These methods will GENERATE an HTML output as a part of the view. They need less coding as it can be reused across the views, so it is advantageous over HTML ELEMENTS. The different built-in helper methods are available that can be used for GENERATING the HTML for a few most commonly used HTML elements such as dropdown list, FORM, checkbox, etc. Along with this, it is also possible to create our own helper methods for custom HTML generation.

A few STANDARD helper methods in MVC are given-below:

  • BeginForm: Used for rendering the HTML form tag
  • TextArea: Used for rendering the text area
  • TextBox: Used for rendering the text box
  • CheckBox: Used for rendering the check box
  • ListBox: Used for rendering the list box
  • DropDownList: Used for rendering the drop-down list
  • Password: Used for rendering the textBox for password input
  • Hidden: Used for rendering the hidden field
  • RadioButton: Used for rendering the radio button
  • ActionLink: Used for rendering the anchor.
9.

Explain RenderSection() in detail.

Answer»

The RenderSection() method belongs to the WebPageBase class and is used for rendering the content of the defined section. The RenderSection() method’s FIRST parameter will be the section name we wish to render at that particular location in the layout template. The second parameter will enable us for defining whether the rendering section is needed or not and this parameter is OPTIONAL. If it is a “required” section, then an error will be thrown by the Razor if that section has not been implemented WITHIN a view template. It returns the HTML content for rendering purposes.

For EXAMPLE, a section named “note” in a layout page can be defined as follows:

@section note{<h1>Header Content</h1>}

The above-defined section can be rendered on the content page by using the below code: @RenderSection("note")

All sections are compulsory by default. To make the sections optional, you need to provide the value of the second parameter as a boolean value false.

<div id="body"> @RenderSection("note", false) //used for injecting the content in the defined section <section class="content-wrapper main-content clear-fix"> @RenderBody() </section></div>

Using the above code, the section named “note” will be rendered on the content page. Here, the RenderBody method can be used for rendering the child page/view.

10.

Explain about ASP.NET MVC life cycle.

Answer»

A Life cycle refers to a sequence of events or steps used for handling some kind of request or modifying the state of an application.

ASP.NET MVC has two life cycles. They are:

  • The Application Life Cycle: The ASP.NET application life cycle begins when a request is raised by a user. It refers to the time when the application process starts running IIS(Internet Information Services) till the time it ends. This is identified as the start and end events of an application in the startup file of your application. On Application Start, the application_start() method will be executed by the webserver. All global variables will be set to default values using this method. On the Application End, the application_end() method will be executed and this will be the final part of the application. The application will be UNLOADED from the memory in this part.
  • The Request Life Cycle: It refers to the series of events that will be executed each time when an HTTP request has been handled by the application. Routing is considered to be an entry point for each MVC application. After the request is received by the ASP.NET platform, it finds out how the request has to be handled with the help of the URL Routing Module. Modules are the components of a .NET that can be attached to the application life cycle. The routing module is helpful in matching the incoming URL(Uniform Resource Locator) to the routes that are defined in our application. All routes will be having a route handler associated with them and this route handler is considered to be an entry point to the MVC FRAMEWORK.

The route data will be converted by the MVC framework into a concrete controller that is capable of handling the requests. The major task after the creation of a controller is Action Execution. An action invoker component will find and select the suitable Action method for invoking the controller.

The next stage after the PREPARATION of the action result is Result Execution. MVC will separate result DECLARATION from result execution. If the result is of view, then it will find and render our view by CALLING the View Engine. If the result is not a view type, the action result will be executed on its own. This Result Execution will generate an original response to the actual HTTP request.