Monday 19 October 2015

MVC Interview Questions and Answers

1. Explain MVC (Model-View-Controller) in general?

MVC (Model-View-Controller) is an architectural software pattern that basically decouples various components of a web application. By using MVC pattern, we can develop applications that are more flexible to changes without affecting the other components of our application.
  •  “Model”, is basically domain data.
  •  “View”, is user interface to render domain data.
  •  “Controller”, translates user actions into appropriate operations performed on model.
MVC Architecture
2. What is ASP.NET MVC?
ASP.NET MVC is a web development framework from Microsoft that is based on MVC (Model-View-Controller) architectural design pattern. Microsoft has streamlined the development of MVC based applications using ASP.NET MVC framework.Understanding ASP.NET MVC
3. Difference between ASP.NET MVC and ASP.NET WebForms?
ASP.NET Web Forms uses Page controller pattern approach for rendering layout, whereas ASP.NET MVC uses Front controller approach. In case of Page controller approach, every page has its own controller i.e. code-behind file that processes the request. On the other hand, in ASP.NET MVC, a common controller for all pages processes the requests.

ASP.NET Web Forms



ASP.NET MVC

ASP.NET Web Forms uses Page controller pattern approach for rendering layout. In this approach, every page has it’s own controller i.e. code-behind file that processes the request.
ASP.NET MVC uses Front Controller approach. That approach means ,a common controller for all pages, processes the requests.
No separation of concerns. As we discussed that every page (.aspx) has it’s own controller (code behind i.e. aspx.cs/.vb file), so both are tightly coupled.
Very clean separation of concerns. View and Controller are neatly separate.
Because of this coupled behavior, automated testing is really difficult.
Testability is key feature in ASP.NET MVC. Test driven development is quite simple using this approach. Please follow here for demo on building testable applications.
In order to achieve stateful behavior, viewstate is used. Purpose was to give developers, the same experience of a typical WinForms application.
ASP.NET MVC approach is stateless as that of the web. So here no concept of viewstate.
Statefulness has a lots of problem for web environment in case of excessively large viewstate. Large viewstate means increase in page size.
As controller and view are not dependent and also no viewstate concept in ASP.NET MVC, so output is very clean.
ASP.NET WebForms model follows a Page Life cycle.
No Page Life cycle like WebForms. Request cycle is simple in ASP.NET MVC model.
Along with statefulness, microsoft tries to introduce server-side controls as in Windows applications. Purpose was to provide  somehow an abstraction to the details of HTML. In ASP.NET Web Forms, minimal knowledge of HTML, JavaScript and CSS is required.
In MVC, detailed knowledge of HTML, JavaScript and CSS is required.
Above abstraction was good but provides limited control over HTML, JavaScript and CSS which is necessary in many cases.
Full control over HTML, JavaScript and CSS.
With a lots of control libraries availability and limited knowledge of other related technologies, ASP.NET WebForms is RAD(Rapid Application Development) approach.
It’s a step back. For developers decrease in productivity.
It’s good for small scale applications with limited team size.
It’s better as well as recommended approach for large-scale applications where different teams are working together.

4. Is MVC different from a three layered architecture?

MVC is an evolution of a three layered traditional architecture. Many components of the three layered architecture are part of MVC. So below is how the mapping goes:
FunctionalityThree layered / tiered architectureModel view controller architecture
Look and FeelUser interfaceView
UI logicUser interfaceController
Business logic /validationsMiddle layerModel
Request is first sent toUser interfaceController
Accessing dataData access layerData Access Layer

5. What are the Core features of ASP.NET MVC?
Core features of ASP.NET MVC framework are:
·         Clear separation of application concerns (Presentation and Business Logic). It reduces complexity that makes it ideal for large scale applications where multiple teams are working.
·         It’s an extensible as well as pluggable framework. We can plug components and further customize them easily.
·         It provides extensive support for URL Routing that helps to make friendly URLs (means friendly for human as well as Search Engines).
·         It supports for Test Driven Development (TDD) approach. In ASP.NET WebForms, testing support is dependent on Web Server but ASP.NET MVC makes it independent of Web Server, database or any other classes.
·         Support for existing ASP.NET features like membership and roles, authentication and authorization, provider model and caching etc.


5. Can you please explain the request flow in ASP.NET MVC framework?
Request flow for ASP.NET MVC framework is as follows:
Request hits the controller coming from client. Controller plays its role and decides which model to use in order to serve the request. Further passing that model to view which then transforms the model and generate an appropriate response that is rendered to client.
ASP.NET MVC Request Flow

6. What is Routing in ASP.NET MVC?
In case of a typical ASP.NET application, incoming requests are mapped to physical files such as .aspx file. On the other hand, ASP.NET MVC framework uses friendly URLs that more easily describe user’s action but not mapped to physical files. Let’s see below URLs for both ASP.NET and ASP.NET MVC.

//ASP.NET approach Pointing to physical files(Student.aspx)

//Displaying all students

http://locahost:XXXX/Student.aspx

//Displaying a student by Id = 5

http://locahost:XXXX/Student.aspx?Id=5

//ASP.NET MVC approach Pointing to Controller i.e. Student

//Displaying all students

http://locahost:XXXX/Student

//Displaying student by Id = 5

http://locahost:XXXX/Student/5/

ASP.NET MVC framework uses a routing engine, that maps URLs to controller classes. We can define routing rules for the engine, so that it can map incoming request URLs to appropriate controller.
Practically, when a user types a URL in a browser window for an ASP.NET MVC application and presses “go” button, routing engine uses routing rules that are defined in Global.asax file in order to parse the URL and find out the path of corresponding controller.

No comments:

Post a Comment