Saturday, 26 October 2013

MVC - Interview Questions and Answers

What is MVC?
    MVC stands for Model View Controller. 
It divides an application into 3 component roles which is based on a framework methodology. 
These component roles are discussed briefly as follows: 

i) Models: These component roles are used to maintain the state which is persisted inside the Database. 

ii) Views: These component roles are used to display the user interface of the application, where this UI is created off of the model data. 

iii) Controllers: These component roles are used for various purposes like handling end user interaction, manipulating the model, and ultimately choosing a view to render to display UI. 

Can we share a view across multiple controllers ?
            Yes, It is possible to share a view across multiple controllers by putting a view into the          shared folder. 

Return Type of Controller Action Method ?
i) ViewResult : It is used to return a webpage from an action method 
ii) PartialViewResult : It is used to send a section of a view to be rendered inside another view. 
iii) JavaScriptResult : It is used to return JavaScript code which will be executed in the user’s browser. 
iv) RedirectResult : Based on a URL, It is used to redirect to another controller and action method. 
v) ContentResult : It is an HTTP content type may be of text/plain. It is used to return a custom content type as a result of the action method. 
vi) JsonResult : It is used to return a message which is formatted as JSON. 
vii) FileResult : It is used to send binary output as the response. 
viii) EmptyResult : It returns nothing as the result.

The page lifecycle of an ASP.NET MVC page is explained as follows: 
i)                    Application Initialisation 
In this stage, the application starts up by running Global.asax’s Application_Start() method. 

ii) Routing 
Routing is a stand-alone component that matches incoming requests to IHttpHandlers by URL pattern. 
MvcHandler is, itself, an IHttpHandler, which acts as a kind of proxy to other IHttpHandlers configured in the Routes table. 

iii) Instantiate and Execute Controller 
At this stage, the active IControllerFactory supplies an IController instance. 
iv) Locate and invoke controller action 
At this stage, the controller invokes its relevant action method, which after further processing, calls RenderView(). 

v) Instantiate and render view 
At this stage, the IViewFactory supplies an IView, which pushes response data to the IHttpResponse object.

Difference Between ViewData, ViewBag and TempData
 A ViewData is a Dictionary to stored value from controller action and pass it to the View.
A ViewBag is a Collection of View Data , it is dynamic Value pass from controller action to view.        
A TempData is a Dictionary is used to pass the data between the Actions.


  1. tnks for sharing everythnk in .net sir its very useful to as
    Dot Net Training institutes in Chennai

  2. This comment has been removed by a blog administrator.

  3. This comment has been removed by a blog administrator.