Interview Question 1: What is MVC?
MVC is an architectural pattern which separates the representation and user interaction. It’s divided into three broader sections, Model, View, and Controller. Below is how each one of them handles the task.
- The View is responsible for the look and feel. It is the presentation layer in MVC.
- 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
Interview Question 2: What is ASP.NET MVC?
[amazon_textlink asin=’1430265299|1484221362′ text=’ASP.NET MVC’ template=’ProductLink’ store=’desibanjara22-21|desibanjaraco-21′ marketplace=’IN|UK’ link_id=’e8db020a-b46a-11e8-bb61-0b10f0770be6′] is a web application Framework based on the MVC architectural pattern. It is light weight and highly testable Framework. MVC separates application into three components — Model, View and Controller.
The MVC model also provides full control over HTML, CSS, and JavaScript.
The MVC separation helps developers to manage complex applications because we can focus on one aspect a time. For example, you can focus on the view without depending on the business logic. It also makes it easier to test an application.
Interview Question 3: What are the benefits of using MVC? Or
What are the advantages of MVC over ASP.NET?
There are two big benefits of MVC:
- Provides a clean separation of concerns among UI (Presentation layer), model (Domain Objects/Entities) and Business Logic (Controller).
- By moving the binding code to a separate class we can also reuse the code to a great extent. We can have multiple views which can point to the same model and vice versa.
- Automated UI testing is possible because now the behind code (UI interaction code) has moved to a simple .NET class. This gives us opportunity to write unit tests and automate manual testing.
- Improved structuring of the code.
Interview Question 4: Can you explain the page life cycle of MVC?
Below are the sequence of processes in ASP.NET MVC life cycle:-
- App initialization
- Routing
- Instantiate and execute controller
- Locate and invoke controller action
- Instantiate and render view.
Interview Question 5: What is Razor View Engine?
Razor was the first major update to render HTML in MVC 3. Razor was designed specifically for view engine syntax. Main focus of Razor was to simplify and code-focused templating for HTML generation.
Interview Question 6: What is the meaning of Unobtrusive JavaScript?
This is a general term that conveys a general philosophy, similar to the term REST (Representational State Transfer). Unobtrusive JavaScript doesn’t intermix JavaScript code in your page markup.
Example: Instead of using events like onclick and onsubmit, the unobtrusive JavaScript attaches to elements by their ID or class based on the HTML5 data- attributes.
Interview Question 7: What is the use of ViewModel in MVC?
ViewModel is a plain class with properties, which is used to bind it to strongly typed view. ViewModel can have the validation rules defined for its properties using data annotations.
Interview Question 8: What are Actions in MVC?
Actions are the methods in Controller class which is responsible for returning the view or json data. Action will mainly have return type — “ActionResult” and it will be invoked from method — “InvokeAction()” called by controller.
Interview Question 9: How can we maintain sessions in MVC?
In MVC sessions can be maintained by three ways: tempdata, viewdata, and viewbag.
Interview Question 10: What is ViewData?
Viewdata contains the key, value pairs as dictionary and this is derived from class — “ViewDataDictionary“. In action method we can set the value for viewdata and in view the value will be fetched by typecasting.
Viewdata maintain state between controller and corresponding view. Just only for single request. It will become null in further request or if redirect occurs.
Interview Question 11: What is the difference between ViewBag and ViewData in MVC?
ViewBag is a wrapper around ViewData, which allows to create dynamic properties. Advantage of viewbag over viewdata will be –
In ViewBag no need to typecast the objects as in ViewData.
ViewBag will take advantage of dynamic keyword which is introduced in version 4.0. But before using ViewBag we have to keep in mind that ViewBag is slower than ViewData.
Interview Question 12: Explain TempData in MVC?
TempData is again a key, value pair as ViewData. This is derived from “TempDataDictionary” class. TempData is used when the data is to be used in two consecutive requests, this could be between the actions or between the controllers. This requires typecasting in view.
[amazon_link asins=’B00TQXRQKY|1788293606′ template=’ProductAd’ store=’desibanjara22-21|desibanjaraco-21′ marketplace=’IN|UK’ link_id=’40c039e2-b46b-11e8-8159-394253d23a30′]
Interview Question 13: How can we do validations in MVC?
One of the easiest ways of doing validation in MVC is by using data annotations. Data annotations are nothing but attributes which can be applied on model properties.
Interview Question 14: What you mean by Routing in MVC?
Routing is a pattern matching mechanism of incoming requests to the URL patterns which are registered in route table.
Interview Question 15: What is Attribute Routing in MVC?
Attribute Routing is introduced in MVC5. ASP.NET Web API also supports this type routing. In this type of routing, attributes are being used to define the routes. This type of routing gives more control over classic URI Routing. Attribute Routing can be defined at controller level or at Action level.
Interview Question 16: How to enable Attribute Routing?
Just add the method — “MapMvcAttributeRoutes()” to enable attribute routing as shown below
public static void RegistearRoutes(RouteCollection routes)
{
routes.IgnoareRoute(“{resource}.axd/{*pathInfo}”);
//enabling attribute routing
routes.MapMvcAttributeRoutes();
//convention-based routing
routes.MapRoute
(
name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Customer”, action = “GetCustomerList”, id = UrlParameter.Optional }
);
}
Interview Question 17: Explain JSON Binding?
JavaScript Object Notation (JSON) binding support started from MVC3 onwards via the new JsonValueProviderFactory, which allows the action methods to accept and model-bind data in JSON format. This is useful in Ajax scenarios like client templates and data binding that need to post data back to the server.
Interview Question 18: How to send result back in JSON format in MVC?
JsonResult class by which we can return back data in JSON format.
Interview Question 19: Explain 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.
Interview Question 20: What are HTML helpers in MVC?
HTML Helpers are like controls in traditional web forms. It help you to render HTML controls in the view. But HTML helpers are more lightweight compared to web controls as it does not hold viewstate and events.
HTML Helpers returns the HTML string which can be directly rendered to HTML page. Custom HTML Helpers also can be created by overriding “HtmlHelper” class.
Interview Question 21: What is the difference between “HTML.TextBox” vs “HTML.TextBoxFor”?
HTML.TextBoxFor” is strongly typed while “HTML.TextBox” isn’t.
[amazon_link asins=’1430265299,B00SWFNGOW,9352130936,B01IF63FIY,8183335810,1786463830,8126551925,9350041995,B00TQXRQKY|1484221362,1118794753,1788476638,148423149X,1430265299,1788293606,1430265418,1535167866,1546832068′ template=’ProductGrid’ store=’desibanjara22-21|desibanjaraco-21′ marketplace=’IN|UK’ link_id=’dabc816c-b46a-11e8-8330-fb8992d8449c’]