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 are the advantages of using Scaffolding in ASP.NET MVC?

Answer»

Scaffolding is a code generation FRAMEWORK used for ASP.NET web applications that will be helpful in effectively generating the code for CRUD(CREATE, Read, Update, and Delete) operations against your database.

The Advantages of Scaffolding are as follows:

  • Minimal or no code for the CREATION of DATA-driven Web applications
  • Pages will be completely functional and will have various functionalities such as insert, edit, display, delete, paging, and sorting
  • Faster development time
  • Filters will be created for every boolean or foreign key field
  • The database schema BASED built-in data validation.
2.

How to implement Forms authentication in ASP.NET MVC?

Answer»

AUTHENTICATION means providing access to the user for a particular service through verification of his/her identity using his/her credentials such as email-id and password or username and password. It will make sure that the correct user will be authenticated or logged in for a particular service and the right service will be GIVEN to the specific user depending on their role.

Forms authentication in ASP.NET will be done after the completion of IIS(Internet Information Services) authentication. It is possible to configure forms authentication with the help of the forms element in the file web.config of your application. 

The forms authentication default attribute values are given below:

<system.web&GT; <authenticationmode="Forms"> <formsloginUrl="Login.aspx" protection="All" timeout="30" name=".ASPXAUTH" path="/" requireSSL="false" slidingExpiration="true" defaultUrl="default.aspx" cookieless="UseDeviceProfile" enableCrossAppRedirects="false" /> </authentication></system.web>

The FormsAuthentication class will automatically create the authentication cookie when it calls the methods RedirectFromLoginPage() or SetAuthCookie(). The value of an authentication cookie will be having the STRING representation for the signed and encrypted FormsAuthenticationTicket object.

3.

Explain state management in ASP.NET MVC.

Answer»

The collection of input values(example: city, mobile number, e-mail id, etc.) of a specific web page is called as state of that page. By default, it is not possible to retain the values(states) provided on a single page of a web APPLICATION, when you navigate to another web page. This nature of a web page is known as stateless behaviour.

You can make use of different state management techniques for retaining the values(state) across the web pages. The below-given COMMON state management techniques will be supported by the ASP.NET MVC:

  • Session: It is a state management technique on the server-side that enables you for setting and read the user-specific values when the user navigates through different pages in a web application. These state variables will be stored by default in the host application’s MAIN memory. It is also possible to configure this session variable to store on a SQL server DATABASE. The Session.Clear() method, can be used for removing the session variable from memory.
  • Query String: It is a state management technique on the client-side that is used for storing the values along with URL(Uniform Resource Locator). It cannot be used for storing the composite type values and also for storing confidential data such as passwords, as a query string area will be VISIBLE as part of the URL.
  • Cookies: It is a state management technique on the client-side that stores data in browsers memory. Based on settings, a few browsers may not be able to accept cookies.
4.

Explain TDD(Test Driven Development) in ASP.NET MVC.

Answer»

TDD or Test DRIVEN DEVELOPMENT is an approach where a test will be written before the completion of production code writing for the purpose of refactoring and fulfilling the test. TDD is also known as the Red-Green-Refactor method of development.

The Workflow of TDD is given below:

  • Requirement identification
  • Write a test case for automation
  • Tests will be executed and you need to MAKE sure that the newer tests fail(red colour)
  • Write the complete production code
  • All tests have to be executed and passed.
  • Refactoring will be performed
  • The process should be repeated.
5.

Explain about TempData in ASP.NET MVC.

Answer»

TempData is a dictionary object useful for the temporary storage of data. It is an instance of the Controller base class. It is useful in storing the data for the duration of an HTTP request, or we can SAY that it can store live data between two consecutive REQUESTS of HTTP. It is also useful in passing the state between ACTION methods. TempData can be used only with the subsequent and current requests. It will make use of a session variable for storing the data. TempData needs typecasting during data retrieval.

Example: In the below-given code, employee details will be TEMPORARILY stored by TempData.

public ActionResult FirstRequest(){ LIST<string> TempDataDemo = new List<string>(); TempDataDemo.Add("Harish"); TempDataDemo.Add("Nagesh"); TempDataDemo.Add("Rakshith"); TempData["EmployeeName"] = TempDataDemo; return View();}public ActionResult ConsecutiveRequest(){ List<string> modelData = TempData["EmployeeName"] as List<string> ; TempData.Keep(); return View(modelData);}
6.

What is Razor View Engine in ASP.NET MVC?

Answer»

Razor View Engine in ASP.NET MVC is a simple-syntax view engine used for creating a view of an application. It was released in JANUARY 2011 for Microsoft Visual Studio as a part of ASP.NET MVC 3.

Razor will make USE of C# syntax for the PURPOSE of embedding code in a page without the usage of ASP.NET delimiters: <%= %>. The file extension of Razor is “cshtml” for the C# PROGRAMMING language. It supports Test Driven Development(TDD) as it will not be dependent on the System.Web.UI.Page class.

7.

What is View Engine?

Answer»

A View Engine is an MVC subsystem and has its own MARKUP SYNTAX. It will convert the template from the server-side into HTML markup and then render it to the browser. In the BEGINNING, ASP.NET MVC was having a single view engine i.e., web forms(ASPX), from ASP.NET MVC 3 a Razor view engine Razor has been INTRODUCED. You can also make use of other view engines such as NHaml, Spark, etc., in ASP.NET MVC.

8.

What are bundling and minification in ASP.NET MVC?

Answer»

Bundling and minification are two techniques used for your WEB application in the ASP.NET MVC framework for improving the performance of the PAGE load. Bundling will combine several files into a single file. Minification will perform different TYPES of code optimizations to CSS and scripts, which will result in smaller payloads. With the help of bundling and minification, we can improve the performance of load time by reducing the requested ASSETS(like JavaScript and CSS files) size and by reducing the number of requests to the server.

9.

What is ViewBag and ViewData in ASP.NET MVC?

Answer»

ViewBag:

  • It is a Wrapper developed around ViewData and is a dynamic property that uses dynamic features of C# 4.0.
  • ViewBag is mainly used for the passing of value from the Controller to View where it does not require to Type Caste the data during data retrieval.
  • This ViewBag is available only for the Current Request and will be DESTRUCTED on re-direction.
  • Example:
    In the below given example, a string value will be set by the ViewBag object in Controller and later it will be displayed with the help of View.

Controller:

public CLASS ExampleController : Controller{ public ActionResult Index() { ViewBag.Message = "This is about ViewBag ASP.NET MVC!!"; return View(); }}

 View:

<html><head> <meta name="viewport" content="width=device-width"/> <title>IndexDisplay</title></head><body> <div> @ViewBag.Message </div></body></html>

ViewData:

  • It is derived from the class ViewDataDictionary and is originally a Dictionary object, which means it has Keys and Values where Keys represents the String and Values will be objects. Data will be stored in the form of Objects in ViewData.
  • ViewData is mainly used for the passing of value from the Controller to View. During retrieval of data, it needs to typecast the data into its ORIGINAL type since the data will be stored in the form of objects and it also needs NULL checks during data retrieval.
  • Similar to viewBag, this viewData is available only for Current Request and will be destructed upon redirection.
  • Example:
    In the below-given code, a string value will be set by the ViewData object in the Controller and later it will be displayed by the View.

Controller:

public class ExampleController : Controller{ public ActionResult Index() { ViewData["Message"] = "This is about ViewData in ASP.NET MVC!!!"; return View(); }}

  View:

<html><head> <meta name="viewport" content="width=device-width"/> <title>Index</title></head><body> <div> @ViewData["Message"] </div></body></html>
10.

What are the Advantages of ASP.NET MVC?

Answer»

Following are a few advantages of ASP.NET MVC:

  • Separation of Concerns: The ASP.NET MVC framework will give you a clean separation of the USER Interface(UI), Business Logic, Model/Data. That means it will give you a complete separation of Program logic from the UI.
  • Lightweight: The ASP.NET MVC framework will not make use of View State, it will result in bandwidth reduction of the REQUESTS to some extent.
  • Testability: The ASP.NET MVC framework will provide you with better Web application testability and will ALSO give you very good SUPPORT for test-driven development.
  • More Control: The ASP.NET MVC framework will support having control over JavaScript, CSS, and HTML in comparison with the traditional WebForms.
  • All features of ASP.NET: ASP.NET MVC framework is built on the top of ASP.NET framework, thus almost all the ASP.NET features such as roles, membership providers, etc., can be still USED.
11.

What are Action filters in ASP.NET MVC?

Answer»

ACTION filters are the additional ATTRIBUTES in the ASP.NET MVC framework, that we can apply to either a section of the CONTROLLER or to the entire controller for modifying the approach in which action will be executed. Action filter attributes are SAID to be special .NET classes as they are derived from the System.Attribute class and they can be ATTACHED to classes, properties, methods, and fields.

The following action filters are provided by ASP.NET MVC:

  • Output Cache: This action filter will cache the controller action output for a specified time.
  • Handle Error: This action filter will handle the errors raised during the execution of a controller action.
  • Authorize: This action filter will enable you for restricting to a particular role or user.

The below-given code will apply Output cache filter on an ActionFilterExampleController (Here, ActionFilterExampleController is just used as an example, You can apply action filters on any of the controllers). It specifies that the return value should be cached for 20 seconds.

public class ActionFilterExampleController: Controller{ [HttpGet] OutputCache(Duration = 20)] public string Index() { return DateTime.Now.ToString("T"); }}
12.

Explain about ASP.NET MVC filters.

Answer»

The five types of Filters in ASP.NET MVC and order of their execution are as follows:

  • Authentication Filters: It executes before the execution of any other filter or action method. Authentication filter will make sure about whether you are a valid or INVALID user. These filters will implement the interface IAuthenticationFilter.
  • Authorization Filters: It is responsible for checking User Access and is useful in implementing the authentication as well as authorization for controller actions. These authorization filters will implement the interface IAuthorizationFilterinterface. Examples of an Authorization filter are:
       - AuthorizeAttribute: It restricts access by optionally authorization and authentication.
       - RequireHttpsAttribute: It forces HTTP(HyperText Transfer Protocol) requests that are unsecured to be resent over HTTPS (HyperText Transfer Protocol Secure).
  • Action Filters: It is an attribute that can be applied to a controller action or a complete controller. It will be called before the execution of action begins and after the execution of action COMES to end. These action filters will implement the interface IActionFilter in the framework, which has OnActionExecuting and OnActionExecuted methods. OnActionExecuting method executes before the Action and provides an opportunity for Action call cancelling. Since action filters are executed before and after a controller action executes, they can also be used for modifying the view data that is returned by a controller action.
  • Result Filters: It contains a logic that is to be executed before and after the execution of a view result. That means if you wish to modify a view result exactly before the rendering of view to the browser. These result filters are implemented from the IResultFilter interface in the framework, which has two methods namely OnResultExecuting and OnResultExecuted. An example of a result Filter is OutputCacheAttribute class which will provide OUTPUT caching.
  • ExceptionFilters: It can be used for handling the ERRORS that are raised by the controller actions or by the results of controller actions. The exception filters will implement the IExceptionFilter interface in the framework. It is very helpful during the execution pipeline where any unhandled EXCEPTIONS might be thrown. An example for ExceptionFilter is HandleErrorAttribute class which will specify the way for handling an exception thrown by an action method.

It is possible to override the methods in your controller class if you wish to do so.

13.

What is JsonResultType (JavaScript Object Notation Result Type) in ASP.NET MVC?

Answer»

Action methods on controllers will return JsonResult that will be used WITHIN an AJAX application. From the “ActionResult” abstract CLASS, this JsonResult class will be inherited. Here Json will be supplied with a SINGLE argument that must be serializable. The JSONResult OBJECT will SERIALIZE the specified object into JSON format.

Example:

public JsonResult JsonResultExample(){ return Json("Hello InterviewBit!!");}
14.

What is Area in ASP.NET MVC?

Answer»

The area will permit to divide the larger application into smaller units where every UNIT will have an MVC FOLDER structure SEPARATELY, exactly like the default MVC folder structure. 

Consider an example of a large enterprise application that might have different modules such as HR, finance, ADMIN, marketing, etc. So for all these modules, an Area can have a separate MVC folder structure.

15.

What is the Action Method in MVC?

Answer»

The action methods in ASP.NET MVC will help for executing the request and generating a response to it. Every PUBLIC method of the MVC Controller is considered an action method. If you want the public method to act as a non-action method, then it is possible to decorate the action method by using the ATTRIBUTE “NonAction”. This will restrict an action method from rendering on the browser.

A few points to be remembered while working with action methods are:

  • The action method is the public and non-static method
  • It cannot be the protected or private method
  • It cannot be an extension method
  • It can’t have ref and out parameters
  • It cannot be overloaded

It is possible to create action methods that will return an object of any type like an integer, a Boolean value, or a string. These return types will be wrapped in an ActionResult type that is appropriate before they are being RENDERED onto the response stream. Any return type that is not an action result will be converted into a string by the ASP.NET MVC and the string will be rendered into the browser. We can create a simple controller as PER the code given below:

public class HomeController : Controller { public ActionResult HelloInterviewbit() { ViewData["SayInterviewbit"] = "Interviewbit"; return View(); } public string SayInterviewbit() { return "This is from 'SayInterviewbit'."; } }

We must create a view for running SayInterviewbit() as it will return the View as views\home\SayInterviewbit.cshtml. For executing the second action method, it does not require the creation of a view. In the browser, the string will be rendered. It is possible for an void public method to act as an action method. This implies that any public method can BECOME an action method.

16.

When to use Attribute Routing?

Answer»

By using attribute routing, you can easily define some of the URI patterns that are most COMMON in RESTful APIs. For example, resources will usually have child resources such as movies have actors, Clients will have orders, books have authors, etc. So it’s obvious to create URIs that reflect these kinds of relations such as /clients/1/orders. It is difficult to create these kinds of URIs using convention-based routing. With the help of attribute routing, it becomes easier for defining a route to this URI. 

You need to ADD an attribute to the controller action as GIVEN below:

[Route("clients/{cId}/orders")]public IEnumerable<Order> ObtainOrdersByClient(int cId){ //Statements}

To enable attribute routing in ASP.NET MVC 5 application, you need to add a call to the method routes.MapMvcAttributeRoutes() WITHIN the RegisterRoutes() method of RouteConfig.cs file.

17.

What is Routing in ASP.NET MVC?

Answer»

Routing in ASP.NET MVC is a pattern matching system that maps the incoming requests from the BROWSER to specified actions of the MVC controller. When the application of ASP.NET MVC gets started, it registers one or multiple patterns with the framework route table so that it can inform the routing engine that what has to be done with the requests that are matching with those patterns. When a request is received by the routing engine during runtime, it matches that URL of the request against its registered URL patterns and PROVIDES the response based on a pattern match.

The URL pattern in routing will be considered based on the domain name part in the URL. For example, the URL pattern "{controller}/{action}/{id}" will look similar to localhost:1234/{controller}/{action}/{id}. Anything that comes after the"localhost:1234/" will be considered as the name of the controller. In the same manner, anything that comes after the controller name will be considered as the name of the action and then comes the id parameter value.

If the URL has nothing after the domain name, then the request will be handled by the default controller and action method. For example, the URL http://localhost:1234 will be handled by the Index() method and by the HomeController as per the default parameter configuration.

The below-given route table will show which CONTROLLERS, Action methods, and Id parameters will be HANDLING the various URLs according to the above-provided default route.

The below figure shows request processing by routing engine and response sent by it. It provides a response based on URL match in the route table. When the URL of the request matches any of the route patterns that are registered in the route table then the request will be forwarded by the routing engine to the suitable handler for that request. Later the route will be processed and obtain a view on the UI. When the URL of the request doesn’t match with any of the route patterns that are registered, the routing engine will indicate that it can’t determine how to do request handling by returning an HTTP STATUS code 404.

18.

Explain about ASP.NET MVC 5.

Answer»

The ASP.NET MVC 5 FRAMEWORK is the NEWEST evolution of the ASP.NET web platform by Microsoft. A HIGHLY effective programming model will be provided by ASP.MVC 5, which promotes powerful extensibility, cleaner code architecture, test-driven development, in addition to all the ASP.NET BENEFITS.

19.

What are the three main components of an ASP.NET MVC application?

Answer»

In the below-given figure, you can see THREE main components of the MVC architecture in ASP.NET and interaction among them.

The 3 main components of an ASP.NET MVC application are model, VIEW, and controller.

  • Model: Model objects will implement the logic for the data domain of an application and has all the classes that handle data and business logic. Usually, model objects will retrieve and store the model state in the database.
  • Views: View is a component used for displaying the USER Interface(UI) of an application. Generally, this UI will be created using the model data.
  • Controllers: The controller is a component used for handling the user interaction, model-related work, and selecting a view for RENDERING to display the UI. In an MVC application, for displaying the INFORMATION the view will be responsible, for handling and responding to user input and interaction the controller will be responsible.
20.

What is ASP.NET MVC?

Answer»

ASP.NET MVC is a LIGHTWEIGHT framework for developing WEB and mobile applications. This framework divides an application into the three MAIN components that make up MVC(Model, View, and Controller). In this MVC pattern for WEBSITES, action performance or data retrieval will be DONE when requests will be sent to a Controller that is in co-ordinance with the Model for doing such tasks. The model will retrieve the data and also store it in the database. The Controller selects the View for displaying and provides it along with the Model. The final page will be rendered by the View based on the data present in the Model.

MVC along with ASP.NET will give you a powerful and patterns-based way of developing dynamic websites as it enables a separation of concerns.