ASP.NET MVC Interview Questions and Answers

We have compiled most frequently asked .NET Interview Questions which will help you with different expertise levels.

ASP.NET MVC Interview Questions and Answers PDF Free Download

Question 1.
Define MVC.
Answer:
MVC means Model CONTROLLER & view is a software architecture pattern for implementation of the user interface, the MVC is a framework for building a web application using a model controller & design.

Model – the represent the application core for instance a list of database records.

view – the view displays the data records on the user interface.

controller – they handled the input to the database records & MVC is also provides full control over Html, CSS, javascript & angular js, etc.

Question 2.
Write the MVC Advantage.
Answer:
The following advantage of MVC as given below:
a) It makes it easier to manage complexity by dividing an application into the model, the view, and the controller.
b) It does not use view state or server-based forms. This makes the MVC framework ideal for developers who want full control over the behavior of an application.
c) It provides better support for test-driven development (TDD).
d) It uses view state or server-based forms, which can make managing state information easier.
e) It works well for small teams of Web developers and designers who want to take advantage of a large number of components available for rapid application development
f) It uses a Front Controller pattern that processes Web application requests through a single controller. This enables you to design an application that supports a rich routing infrastructure.

Question 3.
What is an abstract class give me an example?
Answer:
They are classes that can’t be instantiated frequently either partly implemented as abstract keywords. Drive class access parent class in abstraction. Members marked as abstract must be implemented by non-abstract classes that derive from the abstract class.
Ex.

Abstract class abd
{
Public abstract int Add number (int x, int y);
}
Abstract class mno:abd
{
Public overriding int add number(int x, int y)
{
return (x+y) }}

Question 4.
What is method overloading explain?
Answer:
Creating the method in a driving class with the same name, same parameters, and same return type as in the base class is called method overriding. The method overriding is an example of run time polymorphism. We can also say dynamic polymorphism.

Ex:

Public class abc
{
Public virtual int bal( )
{
Return 10;
}
Public class abc:abc
{
Public overriding int bal( )
{
Return 200;
}
}

Question 5.
What is method overloading explain?
Answer:
Creating multiple methods in classes with the same name but different parameters and is called method overloading. The method overloading is an example of compile-time polymorphism which is done at compile time. It’s also called static polymorphism & it’s binding early.

Ex:

Public method overloading
{
Public int add (int a, int b)
// 2 int type parameter in this method.
{
return (a+b);
}
Public int add (int a, int b, int c)
//3 int type parameter in this method with the same name.
{
return (a+b+c)
} }

Question 6.
What is partial class explaining?
Answer:
It’s possible to split the definition of class or structure interface or a method over two or more source files. Each source file contains a section of the type or method definition and all parts are combined the application is compiled. The splitting of the class used partial class.

Ex:

Public partial class emp1
{
Public void Work()
{
}
}
Public partial class emp1
{
Public void ToWork()
{
}
}

Question 7.
What is the MVC filter & Explain the filters?
Answer:
The MYC controllers define action methods that usually have a one-to-one relationship with possible user interactions, such as clicking a link or submitting a form. Sometimes you want to perform logic either before an action method is called or after an action method runs. To support this type ASP .Net MVC provide a filter.

a) Authorization Filter: The implementation of authorization filter and make security decisions about whether to execute an action method, such as performing authentication validating properties of the request. The Authorize Attribute class and require HTTPS attribute class are examples of an authorization filter.

b) Action filter: The implementation of actin filter and wrap the action method execution. The action filter interface declares two methods. action Executing and OnAction Executed OnAction Executing run before the action method call and OnAction Executed runs after the action method and can perform additional processing, such as providing extra data to the action method.

Ex: OnAction Executing Method
Void OnActionExecuting (ActionExecuting Context Filter Context)
OnAction Executed Method
Void OnActionExecuted (ActionExecuted Context Filter Context)

c) Result Filter: These implement result filter and wrap execution of the action result object. The resulting filter declares two methods. OnResultExecuting and OnResultExecuted. OnResultExecuting runs before the action result object is executed. OnResultExecuted runs after the result and can perform additional processing of the result such as modifying HTTP response.

d) Exception Filter: The Exception filter implements & execute if there is an unhandled exception throw during the executing of the ASP .NET MCV pipeline. Exception filters can be used for tasks such as logging or display an error page. Ex. HandleError Attribute.

Question 8.
What is Temp Data in MVC, Explain?
Answer:
Temp data in ASP .Net MVC is basically a dictionary object derived from the Temp data dictionary. Temp data say for subsequent HTTP requests as opposed to other options(View data & View Bag) those say only current requests, so Temp data can be used to maintain data between controller action as well as redirection & temp data avoid an error.

Ex:

public Result Temp Emp( ) // Temp emp1
{
Emp empl = new emp
{
EID = “100”,
Name = “Kumar”
};

Temp data [“Emp”] = empl;
Return redirectTo Action (“Pemp”);
}
Public ActionResult Pemp() // Controller action 2 Pemp
{
Emp empl = Tempdata [“emp”] as empl
Return view(empl);
}

Question 9.
Explain the Access modifier?
Answer:
Public: Access anywhere

Private: The type of member can only be accessed by code in the same classes.

Internal: The type or member can only be accessed by any code in the same assembly but no other assembly.

Protected Internal: The type or member can only be accessed by code in the same assembly or by any derived class in another assembly.

Private Protected: The type. or member can be accessed by code in the same class or in a driven class within the base class assembly.

Access Modifier: The Access modifier providing new features for developers can see the visibility of a class property or method.

Question 10.
OOPS, Concept?
Answer:
Class: The class is a collection of objects variable, array, method,s, etc. It’s the blueprint of data, behavior & type.

Object: Object means real-world entity such as pen, book, table, etc.

Inheritance: When one object acquires all the property and behavior of the parent object is know Inheritance.

Abstraction: Hiding internal information and showing functionality is known abstraction. Ex. The phone call we don’t internal process.

Encapsulation: Encapsulation is the process where code and data bind together in a close structure.

Polymorphism: Many forms when one task is performed in different ways is called polymorphism. Ex shape or rectangular. There is two types of polymorphism runtime polymorphism & compile-time polymorphism. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

Question 11.
What is a virtual keyword?
Answer:
The virtual keyword is used to modify a method, property, index or event declare in the base class and allow it to be overridden in the drive class, this method can be overridden by any class that inherits it. By default, methods are non-virtual. You cannot override a non-virtual method.

Ex:

public virtual double Area( )
{
return x * y;
}

Question 12.
What is a partial view?
Answer:
A partial view is a Razor markup file (. cshtml) that renders HTML output within another markup file’s rendered output, partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages. The partial view generically refers to MVC views and Razor Pages as markup files. The partial view is the reusable view that can be used as a child view in multiple views. The use of a partial view is to Break up large markup files into smaller components & Reduce the duplication of common markup content across markup files.

Question 13.
MVC Life Cycle Explain?
Answer:
Routing: The asp .net MVC is nothing but matching incoming URL to action. MVC request work on base of routing data which is in route table. The routing engine forwards the request to the corresponding IRouteHandler for that request. The routing engine returns a 404 HTTP status code against that request if the patterns are not found in the Route Table.

MCV Handler: The MvcHandler is responsible for initiating the real processing inside ASP.NET MVC. MVC handler implements the IHttpHandler interface and further processes the request by using the ProcessRequest method.

Controller: MvcHandler uses the IControllerFactory instance and tries to get an IController instance. If successful, the Execute method is called. The IControllerFactory could be the default controller factory or a custom factory initialized at the Application_Start event.

Action Execution: Once the controller has been instantiated, Controller’s Actionlnvoker determines which specific action to invoke on the controller. Action to be executed is chosen based on attributes ActionNameSelectorAttribute (by default method which has the same name as the action is chosen) and ActionMethodSelectorAttribute

View Result: The action method receives user input, prepares the appropriate response data, and then executes the result by returning a result type. The result type can be ViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult, and EmptyResult.

View Engine: The first step in the execution of the View Result involves the selection of the appropriate View Engine to render the View Result. It is handled by the IViewEngine interface of the view engine. By default, Asp.Net MVC uses WebForm and Razor view engines.

Question 14.
What is two factory Authentication in MVC?
Answer:
Authentication the user in a combination of username and password along with unique OTP by sending on their registered mobile number or email basically Microsoft twillio or ASP SMS & other SMS provided with register users.

Question 15.
List the different return types of controllers of the Action method?
Answer:

SL ActionResult HelperMethod Description
1 ViewResult View ViewResult Renders a view as a web page.
2 Partial ViewResult Partial view As the name describes PartialViewResult renders the partial view.
3 RedirectResult Redirect When you want to redirect to another action method we will                           use

RedirectResult

4 RedirectT oRouteResult RedirectoRoute Redirect to another action method
5 ContentResult Content Returns a user-defined content type
6 JsonResult Json When you want to return a serialized JSON object
7 JavaScriptResult Javascript Returns a script that can be executed on the client.
8 FileResult File Returns a binary output to write to the response
9 EmptyResult None Returns null result

Question 16.
The difference between Temp data & View Data in MVC.
Answer:

Temp Data View Data
Temp data is derived from the temp data dictionary. View data is used to pass data from controller to view.
Temp data is used to pass data from the current request to the next request. It is derived from the view data dictionary class.
It keeps the information for the time of HTTP request. It’s available for the current request only.
TempData is used to pass data between two consecutive requests. Its value becomes null if a redirection has occurred.

Question 17.
What is Razor in MVC?
Answer:
Razor is one of the view engines supported in ASP.NET MVC. Razor allows you to write a mix of HTML and server-side code using C# or Visual Basic. Razor view with visual basic syntax has .vbhtml file extension and C# syntax has .cshtml file extension.

Question 18.
Explain Scaffolding in MVC?
Answer:
It is a feature of ASP.NET that allows us to generate functional code rapidly. It is also known as a code generator framework. It is pre-installed in Visual Studio 2013 and higher versions. To create a basic CRUD application, scaffolding is the best choice. It reduces time amount and generate clean code. Here, we are using scaffolding to develop the CRUD application.

Question 19.
Difference between view data and view bag?
Answer:
View Data:

  • ViewData is the property of ControllerBase class.
  • ViewData is a type of dictionary object.
  • ViewData is a key-value dictionary collection.
  • ViewData was introduced in MVC 1.0 version.
  • ViewData works with .Net framework 3.5 and above.
  • Need to do type conversion of code while enumerating.

View Bag:

  • ViewBag is a property of ControllerBase class.
  • ViewBag is a type of dynamic object.
  • ViewBag is a type of object.
  • ViewBag was introduced in MVC 3.0 version.
  • ViewBag works with .Net framework 4.0 and above.

Question 20.
Types of MVC Routing?
Answer:
Conversation-based routing: that type of routing, you define one or more route templates, which are basically parameterized strings. When the framework receives a request, it matches the URI against the route template. We call this style conventional routing because it establishes a convention for URL paths:

  • the first path segment maps to the controller name
  • the second maps to the action name.
  • the third segment is used for an optional id used to map to a model entity

ex: routes.MapRoute(“default”j”{controller=Home}/{act ion=Index}/{id?}”);
Attribute-based routing: Attribute routing uses a set of attributes to map actions directly to route templates. In the following example, app.UseMvc(); is used in the Configure method and no route is passed. The HomeController will match a set of URLs similar to what the default route
Ex: {controller=Home}/{action=Index}/{id?} would match:

Question 21.
What is bundling & Minification MVC.
Answer:
Bundling: A bundle is a logical group of physical files, which loads in a single HTTP request. We have separate CSS files, which can be loaded in a single request with the help of bundling. The bundling also can create for JavaScript files separately. A bundle can’t contain both CSS and JavaScript files. We need to create a separate bundle for CSS and JavaScript files. We create a bundle, based on the use of CSS or JS files in the Application.

Minification: Minification is a technique for removing unnecessary characters (white space, newline, tab), comments, and short variable names from text-based files such as JavaScript and CSS files without expecting to alter functionality to reduce the size, which causes improved load times of a Webpage. There are some tools available to minify JS and CSS files. Ex. IE, Mozilla, Chrome.

Question 22.
Write a few MVC helpers.
Answer:
HtmlHelper class generates Html elements using the model class object in razor view. Ex.

  • Html.ActionLink( )
  • Html.BeginForm( )
  • Html. CheckB ox( )
  • Html.DropDownList( )
  • Html.EndForm( )
  • Html.Hidden( )
  • Html.ListBox( )
  • Html.Password( )
  • Html.RadioButton( )
  • Html.TextArea( )
  • Html.TextBox( )

Question 23.
Why use JSON results in MVC?
Answer:
The JSON format is an open standard format. It’s a very familiar and commonly used concept. It’s a data interchange medium and is very lightweight. Developers use different JSON types for data transformation. JsonResult is an ActionResult type in MVC. It helps to send the content in JavaScript Object Notation (JSON).

• ContentEncoding: It helps to indicate the content encoding type, the default encoding for JSON is UTF-8.
• Content-Type: It helps to indicate the content type. The default content type for JSON is application/JSON; charset=utf-8.
•Data: This indicates what the content data is. That means what you will send in JSON format.
• JsonRequestBehavior: This property has two options. Those are AllowGet and DenyGet. The default option is DenyGet.
• Recursion Limit: Indicates the constraining number of object levels to the process. The default value is 100.

Question 24.
What is Synchronization & Asynchronous in MVC?
Answer:
The synchronous methods for the following conditions:

  • The operations are simple or short-running.
  • Simplicity is more important than efficiency.
  • The operations are primarily CPU operations instead of operations that involve extensive disk or network overhead. Using asynchronous action methods on CPU-bound operations provides no benefits and results in more overhead.

The Asynchronous method for the following conditions:

  • You’re calling services that can be consumed through asynchronous methods, and you’re using .NET 4.5 or higher.
  • The operations are network-bound or I/O-bound instead of CPU-bound.
  • Parallelism is more important than the simplicity of code.
  • You want to provide a mechanism that lets users cancel a long-running request.

Question 25.
What is data annotation in MVC?
Answer:
Data annotations are nothing but certain validation that we put in our models to validate the input from the user, ASP .Net MVC provides a unique feature in which we can validate the models using the data annotation attribute. We can use the data annotation library.

Question 26.
Explain view & Partial View on MVC?
Answer:

  • The Partialview generally does not have any Layout while view content Layout.
  • Partial view mostly does not contain HTML attribute like head, tag, meta, and body while view can have all these attributes
  • Partial view is reusable content that is rendered inside view (parent page)
  • Partial view mostly used with Ajax request to process fast and update particular portion while view load whole page.
  • Partial view generally processes with MVC Ajax.ActionLink and Ajax.Beginform while View process with MVC Html.ActionLink and Html.Beginform.

Question 27.
Strongly type view Example?
Answer:
The strongly type views are used for rendering a specific type of model object instead of using a general view data structure. Ex: <% = model.Name%> <%=modle.Location%>.

Question 28.
What is dependency injection?
Answer:
The Dependency Injection is passing dependency to other objects or frameworks (dependency injector). The Service is made part of the client’s state. The DI can increase readability and code reuse. Dependency injection is one form of the broader technique of inversion of control. It’s part of the SOLID Principle.

Question 29.
What is Property Explain?
Answer:
A Property is a member of the class that provides a flexible mechanism for classes to expose private fields. Internally C# property is special methods called assessors.

Question 30.
What is Design Pattern in C#?
Answer:
Design Patterns may be said as a set of probable solutions for a particular problem that is tested to work best in certain solutions. The use is very hard to predict how the architecture will work for the application when the actual application is built.

Question 31.
Types of Design Pattern?
Answer:
The following types of design patterns are available as given below:

  • Creational Patterns – The patterns deal mainly with the creation of objects and classes.
  • Structural Pattern – The Structural Patterns deals with class and object composition.
  • Behaviors Pattern – There mainly deals with class object communication. That means they are concerned with communication between class and objects.

Question 32.
What is OMR in Entity Frameworks?
Answer:
The Entity Frameworks is an object relation mapper (OMR) that enables .Net Developers to work with a database using a .NET object. It eliminated the need for most of the data access code, which developers usually need to write EF6 is a tried and tested data access technology with many years of features and stabilization.
or
The ORM manages the mapping details between a set of objects and underlying relational databases, XML repositories. ORM hides and encapsulates the change in the data source itself, so that when data sources or their APIs change. The OMR supports NHibernate, Entity Framework, Linq-to-SQL.

Question 33.
What is Software Development Methodology?
Answer:
The following methodology is given below for software development:

a) Agile Methodology- The Agile Methodology & software development work under which requirement and solution involve through the collaborative effort of self-organizing cross-functional teams. It advocates adaptive planning and continuous improvement, and it encourages rapid flexible responses to change.

b) SCRUM – Individual development software. It’s designed for teams of 3 to 9 developers who break their work into actions that can be completed within timeboxed interactions called sprints (typically 2 weeks) and track progress and re-plan in a 15-minute stand-up meeting called SCRUM.

c) Waterfall Model – The waterfall model is a direction & development model that includes analysis, design, construction, testing, development & Maintenance.

Question 34.
What is used to keep and peek at temp data in the MVC projects?
Answer:
The Temp Data once is read to the current request it’s available to a subsequent request. If we want temp data to be read and available in the subsequent request then reading we need to call the “Keep” method in MVC Project. E.g.:

@TempData [“Sonali”];
TempData.keep[“Sonali”];
The peek function help to read as well as advice MVC to maintain “TempData” for the subsequent request.
E.g.:
String str = TempData.peek [“Sonali”].ToStringQ;

Question 35.
Which namespace is used for ASPX View Engine?
A. System.Web.Razor
B. System.Web.Mvc.WebFormViewEngine
C. Both A & B
D. None
Answer:
B. System.Web.Mvc.WebFormViewEngine

Question 36.
Which of the following are Actionselectors?
A. ActionName
B. NonAction
C. ActionVerbs
D. All of the above
Answer:
D. All of the above

Question 37.
What is the name of the configuration files that the App Start folder contains?
A. BundleConfig.es
B. FilterConfig.es
C. RouteConfig.es
D. All of these
Answer:
D. All of these

Question 38.
Attributes can be used for data validation in MVC.
A. DataAnnotations
B. Fluent API
C. DataModel
D. HtmlHelper
Answer:
D. HtmlHelper

Question 39.
RedirectToAction() Method for which Status code represents?
A) 304
B) 302
C) 301
D) 300
Answer:
B) 302

Question 40.
RedirectToActionPermanent() Method for which Status code represents?
A) 304
B) 302
C) 301
D) 300
Answer:
C) 301

Question 41.
In which format data can be return from XML into a table?
A) DataSet
B) Datatable
C) A and B
D) None
Answer:
A) DataSet

Question 42.
Can we use view state in MVC?
A) Yes
B) No
C) Both A & B
D) None
Answer:
B) No

Question 43.
What is default authentication in Internet Information Services (IIS)?
A) Standard User
B) Administrator
C) Anonymous
D) None
Answer:
C) Anonymous

Question 44.
Which Namespace is used for Razor View Engine?
A) System.Web.Razor
B) System.Web.Mvc.WebFormViewEngine
C) Both A & B
D) None
Answer:
A) System.Web.Razor

Question 45.
How can you comment using Razor Syntax?
A) *@ Comment line *@
B) @* Comment line *@
C) @* Comment line @*
D) *@ Comment line @*
Answer:
B) @* Comment line *@

Question 46.
The ASPX View Engine uses to render server-side content.
A) @
B) <%= %>
C) Both A & B
D) None
Answer:
B) <%= %>

Question 47.
Which is faster between ASPX View Engine and Razor View Engine.
A) ASPX View Engine
B) Razor View Engine
C) Both A & B
D) None
Answer:
D) None

Question 48.
What is RouteConfig.es in ASP.Net MVC?
A. RouteConfig.es is used to register MVC config statements, route config.
B. RouteConfig.css is used to register global MVC bundles.
C. None
D. All
Answer:
A. RouteConfig.es is used to register MVC config statements, route config.

Question 49.
What is the purpose of the Interface?
Answer:
The following feature/purpose of the interface is given below:

  • Allow for pluggable software.
  • Allow different objects to interact easily.
  • Hide implementation details of a class from each other.
  • The facility of reuse of software.

Answer:

  • Allow for pluggable software.