MVC Developer Interview Questions And Answers
Download MVC Developer Interview Questions and Answers PDF
Optimize your MVC Developer interview preparation with our curated set of 50 questions. Each question is crafted to challenge your understanding and proficiency in MVC Developer. Suitable for all skill levels, these questions are essential for effective preparation. Don't miss out on our free PDF download, containing all 50 questions to help you succeed in your MVC Developer interview. It's an invaluable tool for reinforcing your knowledge and building confidence.
50 MVC Developer Questions and Answers:
MVC Developer Job Interview Questions Table of Contents:
1 :: Tell me in which assembly is the MVC framework is defined?
The MVC framework is defined in System.Web.Mvc.
Read More2 :: Explain what are Filters in MVC?
In MVC, many times we would like to perform some action before or after a particular operation. For achieving this functionality, ASP.NET MVC provides feature to add pre and post action behaviors on controller's action methods.
Types of Filters:
ASP.NET MVC framework supports the following action filters:
☛ Action Filters: Action filters are used to implement logic that gets executed before and after a controller action executes.
☛ Authorization Filters: Authorization filters are used to implement authentication and authorization for controller actions.
☛ Result Filters: Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.
☛ Exception Filters: You can use an exception filter to handle errors raised by either your controller actions or controller action results. You can also use exception filters to log errors.
Read MoreTypes of Filters:
ASP.NET MVC framework supports the following action filters:
☛ Action Filters: Action filters are used to implement logic that gets executed before and after a controller action executes.
☛ Authorization Filters: Authorization filters are used to implement authentication and authorization for controller actions.
☛ Result Filters: Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.
☛ Exception Filters: You can use an exception filter to handle errors raised by either your controller actions or controller action results. You can also use exception filters to log errors.
3 :: Tell us attribute based routing in MVC?
In ASP.NET MVC 5.0 we have a new attribute route, By using the "Route" attribute we can define the URL structure. For example in the below code we have decorated the "GotoAbout" action with the route attribute. The route attribute says that the "GotoAbout" can be invoked using the URL structure "Users/about".
☰ public class HomeController: Controller
☰ {
☰ [Route("Users/about")]
☰ publicActionResultGotoAbout()
☰ {
☰ return View();
☰ }
☰ }
Read More☰ public class HomeController: Controller
☰ {
☰ [Route("Users/about")]
☰ publicActionResultGotoAbout()
☰ {
☰ return View();
☰ }
☰ }
4 :: Suppose if we have multiple filters, what’s the sequence for execution?
☛ Authorization filters
☛ Action filters
☛ Response filters
☛ Exception filters
Read More☛ Action filters
☛ Response filters
☛ Exception filters
5 :: Tell us what are the Difference between ViewBag&ViewData?
☛ ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
☛ ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
☛ ViewData requires typecasting for complex data type and check for null values to avoid error.
☛ ViewBag doesn't require typecasting for complex data type.
Calling of ViewBag is:
ViewBag.Name = "Hasan";
Calling of ViewData is :
ViewData["Name"] = " Hasan ";
Read More☛ ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
☛ ViewData requires typecasting for complex data type and check for null values to avoid error.
☛ ViewBag doesn't require typecasting for complex data type.
Calling of ViewBag is:
ViewBag.Name = "Hasan";
Calling of ViewData is :
ViewData["Name"] = " Hasan ";
6 :: Tell us what is display mode in MVC?
Display mode displays views depending on the device the user has logged in with. So we can create different views for different devices and display mode will handle the rest.
For example we can create a view “Home.aspx” which will render for the desktop computers and Home.Mobile.aspx for mobile devices. Now when an end user sends a request to the MVC application, display mode checks the “user agent” headers and renders the appropriate view to the device accordingly.
Read MoreFor example we can create a view “Home.aspx” which will render for the desktop computers and Home.Mobile.aspx for mobile devices. Now when an end user sends a request to the MVC application, display mode checks the “user agent” headers and renders the appropriate view to the device accordingly.
7 :: Tell us which approach provides better support for test driven development - ASP.NET MVC or ASP.NET Webforms?
ASP.NET MVC
Read More9 :: Tell me what is the adavantage of using ASP.NET routing?
In an ASP.NET web application that does not make use of routing, an incoming browser request should map to a physical file. If the file does not exist, we get page not found error.
An ASP.NET web application that does make use of routing, makes use of URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.
Read MoreAn ASP.NET web application that does make use of routing, makes use of URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.
10 :: Tell me what is MVC (Model View Controller)?
MVC is a software architecture pattern for developing web application. It is handled by three objects Model-View-Controller. Below is how each one of them handles the task.
☛ The View is responsible for the look and feel.
☛ Model represents the real world object and provides data to the View.
☛ The Controller is responsible for taking the end user request and loading the appropriate Model and View
Read More☛ The View is responsible for the look and feel.
☛ Model represents the real world object and provides data to the View.
☛ The Controller is responsible for taking the end user request and loading the appropriate Model and View
11 :: Explain me what is the difference between each version of MVC 2, 3 , 4, 5 and 6?
MVC 6
☛ ASP.NET MVC and Web API has been merged in to one.
☛ Side by side - deploy the runtime and framework with your application
☛ No need to recompile for every change. Just hit save and refresh the browser.
☛ Dependency injection is inbuilt and part of MVC.
☛ Everything packaged with NuGet, Including the .NET runtime itself.
☛ New JSON based project structure.
☛ Compilation done with the new Roslyn real-time compiler.
MVC 5
☛ Asp.Net Identity
☛ Attribute based routing
☛ Bootstrap in the MVC template
☛ Filter overrides
☛ Authentication Filters
MVC 4
☛ ASP.NET Web API
☛ New mobile project template
Refreshed and modernized default project templates
Many new features to support mobile apps
MVC 3
☛ Razor
☛ HTML 5 enabled templates
☛ JavaScript and Ajax
☛ Support for Multiple View Engines
☛ Model Validation Improvements
MVC 2
☛ Templated Helpers
☛ Client-Side Validation
☛ Areas
☛ Asynchronous Controllers
☛ Html.ValidationSummary Helper Method
☛ DefaultValueAttribute in Action-Method Parameters
☛ Binding Binary Data with Model Binders
☛ DataAnnotations Attributes
☛ Model-Validator Providers
☛ New RequireHttpsAttribute Action Filter
Read More☛ ASP.NET MVC and Web API has been merged in to one.
☛ Side by side - deploy the runtime and framework with your application
☛ No need to recompile for every change. Just hit save and refresh the browser.
☛ Dependency injection is inbuilt and part of MVC.
☛ Everything packaged with NuGet, Including the .NET runtime itself.
☛ New JSON based project structure.
☛ Compilation done with the new Roslyn real-time compiler.
MVC 5
☛ Asp.Net Identity
☛ Attribute based routing
☛ Bootstrap in the MVC template
☛ Filter overrides
☛ Authentication Filters
MVC 4
☛ ASP.NET Web API
☛ New mobile project template
Refreshed and modernized default project templates
Many new features to support mobile apps
MVC 3
☛ Razor
☛ HTML 5 enabled templates
☛ JavaScript and Ajax
☛ Support for Multiple View Engines
☛ Model Validation Improvements
MVC 2
☛ Templated Helpers
☛ Client-Side Validation
☛ Areas
☛ Asynchronous Controllers
☛ Html.ValidationSummary Helper Method
☛ DefaultValueAttribute in Action-Method Parameters
☛ Binding Binary Data with Model Binders
☛ DataAnnotations Attributes
☛ Model-Validator Providers
☛ New RequireHttpsAttribute Action Filter
12 :: Tell me how to implement Forms authentication in MVC?
ASP.NET forms authentication occurs after IIS authentication is completed. You can configure forms authentication by using forms element with in web.config file of your application. The default attribute values for forms authentication are shown below:
☰ <system.web>
☰ <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 creates the authentication cookie automatically when SetAuthCookie() or RedirectFromLoginPage() methods are called. The value of authentication cookie contains a string representation of the encrypted and signed FormsAuthenticationTicket object.
Read More☰ <system.web>
☰ <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 creates the authentication cookie automatically when SetAuthCookie() or RedirectFromLoginPage() methods are called. The value of authentication cookie contains a string representation of the encrypted and signed FormsAuthenticationTicket object.
13 :: Explain me what is the use of Keep and Peek in “TempData”?
Once “TempData” is read in the current request it’s not available in the subsequent request. If we want “TempData” to be read and also available in the subsequent request then after reading we need to call “Keep” method as shown in the code below.
@TempData["MyData"];
TempData.Keep("MyData");
The more shortcut way of achieving the same is by using “Peek”. This function helps to read as well advices MVC to maintain “TempData” for the subsequent request.
string str = TempData.Peek("MyData ").ToString();
Read More@TempData["MyData"];
TempData.Keep("MyData");
The more shortcut way of achieving the same is by using “Peek”. This function helps to read as well advices MVC to maintain “TempData” for the subsequent request.
string str = TempData.Peek("MyData ").ToString();
14 :: Tell us what is JsonResultType in MVC?
Action methods on controllers return JsonResult (JavaScript Object Notation result) that can be used in an AJAX application. This class is inherited from the "ActionResult" abstract class. Here Json is provided one argument which must be serializable. The JSON result object that serializes the specified object to JSON format.
public JsonResult JsonResultTest()
{
return Json("Hello Boos!");
}
Read Morepublic JsonResult JsonResultTest()
{
return Json("Hello Boos!");
}
15 :: What is Bundle.Config in MVC4?
"BundleConfig.cs" in MVC4 is used to register the bundles by the bundling and minification system. Many bundles are added by default including jQuery libraries like - jquery.validate, Modernizr, and default CSS references.
Read More16 :: Tell us where are the routing rules defined in an asp.net MVC application?
In Application_Start event in Global.asax
Read More17 :: Tell us what is the significance of NonActionAttribute?
ASP.NET MVC uses ASP.NET routing, to map incoming browser requests to controller action methods. ASP.NET Routing makes use of route table. Route table is created when your web application first starts. The route table is present in the Global.asax file.
Read More18 :: Explain me what is MVC Application Life Cycle?
Any web application has two main execution steps first understanding the request and depending on the type of the request sending out the appropriate response. MVC application life cycle is not different it has two main phases first creating the request object and second sending our response to the browser.
Creating the request object: -The request object creation has four major steps. Below is the detail explanation.
☛ Step 1 Fill route: MVC requests are mapped to route tables which in turn specify which controller and action to be invoked. So if the request is the first request the first thing is to fill the route table with routes collection. This filling of route table happens in the global.asax file.
☛ Step 2 Fetch route: Depending on the URL sent “UrlRoutingModule” searches the route table to create “RouteData” object which has the details of which controller and action to invoke.
☛ Step 3 Request context created: The “RouteData” object is used to create the “RequestContext” object.
☛ Step 4 Controller instance created: This request object is sent to “MvcHandler” instance to create the controller class instance. Once the controller class object is created it calls the “Execute” method of the controller class.
☛ Step 5 Creating Response object: This phase has two steps executing the action and finally sending the response as a result to the view.
Read MoreCreating the request object: -The request object creation has four major steps. Below is the detail explanation.
☛ Step 1 Fill route: MVC requests are mapped to route tables which in turn specify which controller and action to be invoked. So if the request is the first request the first thing is to fill the route table with routes collection. This filling of route table happens in the global.asax file.
☛ Step 2 Fetch route: Depending on the URL sent “UrlRoutingModule” searches the route table to create “RouteData” object which has the details of which controller and action to invoke.
☛ Step 3 Request context created: The “RouteData” object is used to create the “RequestContext” object.
☛ Step 4 Controller instance created: This request object is sent to “MvcHandler” instance to create the controller class instance. Once the controller class object is created it calls the “Execute” method of the controller class.
☛ Step 5 Creating Response object: This phase has two steps executing the action and finally sending the response as a result to the view.
19 :: Explain me what is the difference between “HTML.TextBox” and “HTML.TextBoxFor”?
Both provide the same HTML output, “HTML.TextBoxFor” is strongly typed while “HTML.TextBox” isn’t. Below is a simple HTML code which just creates a simple textbox with “FirstName” as name.
Html.TextBox("FirstName ")
Below is “Html.TextBoxFor” code which creates HTML textbox using the property name ‘FirstName” from object “m”.
Html.TextBoxFor(m => m.CustomerCode)
In the same way, we have for other HTML controls like for checkbox we have “Html.CheckBox” and “Html.CheckBoxFor”.
Read MoreHtml.TextBox("FirstName ")
Below is “Html.TextBoxFor” code which creates HTML textbox using the property name ‘FirstName” from object “m”.
Html.TextBoxFor(m => m.CustomerCode)
In the same way, we have for other HTML controls like for checkbox we have “Html.CheckBox” and “Html.CheckBoxFor”.
20 :: Explain me what is the difference between Temp data, View, and View Bag?
In ASP.NET MVC there are three ways to pass/store data between the controllers and views.
ViewData
☛ ViewData is used to pass data from controller to view.
☛ It is derived from ViewDataDictionary class.
☛ It is available for the current request only.
☛ Requires typecasting for complex data type and checks for null values to avoid error.
☛ If redirection occurs, then its value becomes null.
ViewBag
☛ ViewBag is also used to pass data from the controller to the respective view.
☛ ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0
☛ It is also available for the current request only.
☛ If redirection occurs, then its value becomes null.
☛ Doesn’t require typecasting for complex data type.
TempData
☛ TempData is derived from TempDataDictionary class
☛ TempData is used to pass data from the current request to the next request
☛ It keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action
☛ It requires typecasting for complex data type and checks for null values to avoid error. Generally, it is used to store only one time messages like the error messages and validation messages
Read MoreViewData
☛ ViewData is used to pass data from controller to view.
☛ It is derived from ViewDataDictionary class.
☛ It is available for the current request only.
☛ Requires typecasting for complex data type and checks for null values to avoid error.
☛ If redirection occurs, then its value becomes null.
ViewBag
☛ ViewBag is also used to pass data from the controller to the respective view.
☛ ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0
☛ It is also available for the current request only.
☛ If redirection occurs, then its value becomes null.
☛ Doesn’t require typecasting for complex data type.
TempData
☛ TempData is derived from TempDataDictionary class
☛ TempData is used to pass data from the current request to the next request
☛ It keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action
☛ It requires typecasting for complex data type and checks for null values to avoid error. Generally, it is used to store only one time messages like the error messages and validation messages
21 :: What is the concept of MVC Scaffolding?
ASP.NET Scaffolding is a code generation framework for ASP.NET Web applications. Visual Studio 2013 includes pre-installed code generators for MVC and Web API projects. You add scaffolding to your project when you want to quickly add code that interacts with data models. Using scaffolding can reduce the amount of time to develop standard data operations in your project.
Scaffolding consists of page templates, entity page templates, field page templates, and filter templates. These templates are called Scaffold templates and allow you to quickly build a functional data-driven Website.
Scaffolding Templates:
☛ Create: It creates a View that helps in creating a new record for the Model. It automatically generates a label and input field for each property in the Model.
☛ Delete: It creates a list of records from the model collection along with the delete link with delete record.
☛ Details: It generates a view that displays the label and an input field of the each property of the Model in the MVC framework.
☛ Edit: It creates a View with a form that helps in editing the current Model. It also generates a form with label and field for each property of the model.
☛ List: It generally creates a View with the help of a HTML table that lists the Models from the Model Collection. It also generates a HTML table column for each property of the Model.
Read MoreScaffolding consists of page templates, entity page templates, field page templates, and filter templates. These templates are called Scaffold templates and allow you to quickly build a functional data-driven Website.
Scaffolding Templates:
☛ Create: It creates a View that helps in creating a new record for the Model. It automatically generates a label and input field for each property in the Model.
☛ Delete: It creates a list of records from the model collection along with the delete link with delete record.
☛ Details: It generates a view that displays the label and an input field of the each property of the Model in the MVC framework.
☛ Edit: It creates a View with a form that helps in editing the current Model. It also generates a form with label and field for each property of the model.
☛ List: It generally creates a View with the help of a HTML table that lists the Models from the Model Collection. It also generates a HTML table column for each property of the Model.
22 :: What is RenderSection in MVC?
RenderSection() is a method of the WebPageBase class. Scott wrote at one point, The first parameter to the "RenderSection()" helper method specifies the name of the section we want to render at that location in the layout template. The second parameter is optional, and allows us to define whether the section we are rendering is required or not. If a section is "required", then Razor will throw an error at runtime if that section is not implemented within a view template that is based on the layout file (that can make it easier to track down content errors). It returns the HTML content to render.
<div id="body">
@RenderSection("featured", required: false)
<section class="content-wrapper main-content clear-fix">
@RenderBody()
</section>
</div>
Read More<div id="body">
@RenderSection("featured", required: false)
<section class="content-wrapper main-content clear-fix">
@RenderBody()
</section>
</div>
23 :: Tell me a few different return types of a controller action method?
The following are just a few return types of a controller action method. In general an action method can return an instance of a any class that derives from ActionResult class.
☛ 1. ViewResult
☛ 2. JavaScriptResult
☛ 3. RedirectResult
☛ 4. ContentResult
☛ 5. JsonResult
Read More☛ 1. ViewResult
☛ 2. JavaScriptResult
☛ 3. RedirectResult
☛ 4. ContentResult
☛ 5. JsonResult
24 :: Tell us what are the advantages of ASP.NET MVC?
☛ 1. Extensive support for TDD. With asp.net MVC, views can also be very easily unit tested.
☛ 2. Complex applications can be easily managed
☛ 3. Seperation of concerns. Different aspects of the application can be divided into Model, View and Controller.
☛ 4. ASP.NET MVC views are light weight, as they donot use viewstate.
Read More☛ 2. Complex applications can be easily managed
☛ 3. Seperation of concerns. Different aspects of the application can be divided into Model, View and Controller.
☛ 4. ASP.NET MVC views are light weight, as they donot use viewstate.
25 :: Tell me how can we do exception handling in MVC?
In the controller you can override the “OnException” event and set the “Result” to the view name which you want to invoke when error occurs. In the below code you can see we have set the “Result” to a view named as “Error”.
We have also set the exception so that it can be displayed inside the view.
public class HomeController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
Exception ex = filterContext.Exception;
filterContext.ExceptionHandled = true;
var model = new HandleErrorInfo(filterContext.Exception, "Controller","Action");
filterContext.Result = new ViewResult()
{
ViewName = "Error",
ViewData = new ViewDataDictionary(model)
};
}
}
To display the above error in view we can use the below code
@Model.Exception;
Read MoreWe have also set the exception so that it can be displayed inside the view.
public class HomeController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
Exception ex = filterContext.Exception;
filterContext.ExceptionHandled = true;
var model = new HandleErrorInfo(filterContext.Exception, "Controller","Action");
filterContext.Result = new ViewResult()
{
ViewName = "Error",
ViewData = new ViewDataDictionary(model)
};
}
}
To display the above error in view we can use the below code
@Model.Exception;