MVC PDF
MVC PDF
NET MVC
INTERVIEW QUESTIONS
1. What is MVC? Explain what is Model-
View-Controller?
MVC is a software architecture pattern for developing web application. It is
handled by three objects Model-View-Controller.
■ Model- It represents the application data domain. In other words
applications business logic is contained within the model and is responsible
for maintaining data
■ View- It represents the user interface, with which the end users
communicates. In short all the user interface logic is contained within the
VIEW
■ Controller- It is the controller that answers to user actions. Based on the user
actions, the respective controller responds within the model and choose a
view to render that display the user interface. The user input logic is
contained with-in the controller
2. What are the advantages of MVC?
There are two types of routing (after the introduction of ASP.NET MVC 5).
■ Convention based routing - to define this type of routing, we call MapRoute method
and set its unique name, url pattern and specify some default values.
■ Attribute based routing - to define this type of routing, we specify the Route attribute
in the action method of the controller.
5. What is Attribute Routing in MVC?
■ ASP.NET MVC5 and WEB API 2 supports a new type of routing, called attribute
routing. As the name implies, attribute routing uses attributes to define routes. In
ASP.NET MVC 5.0 we have a new attribute route. By using the “Route” attribute we
can define the URL structure. For instance, in any code, a user can decorate the
“GotoAbout” action with the route attribute. The route attribute says that the
“GotoAbout” can be invoked using the URL structure “Users/about”. Attribute
routing gives the user more control over the URLs in their web application
■ Exp:
6. What is the difference between
ViewBag and ViewData in MVC?
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.
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.
7. What is PartialView in MVC?
■ Partial view in MVC renders a portion of view content. It is helpful in reducing code
duplication. In simple terms, partial view allows to render a view within the parent
view.
■ PartialView is similar to UserControls in traditional web forms. For re-usability
purpose partial views are used. Since it’s been shared with multiple views these are
kept in shared folder. Partial Views can be rendered in following ways –
■ Html.Partial()
■ Html.RenderPartial()
8. Can a view be shared across multiple
controllers? If Yes, How we can do that?
■ Yes, we can share a view across multiple controllers. We can put the view in the
“Shared” folder. When we create a new MVC Project we can see the Layout page will
be added in the shared folder, which is because it is used by multiple child pages.
9. List out few different return types of
a controller action method?
■ View Result
■ Javascript Result
■ Redirect Result
■ Json Result
■ Content Result
10. Mention what is the difference between
"ActionResult" and "ViewResult"?
■ "ActionResult" is an abstract class while "ViewResult" is derived from
"AbstractResult" class. "ActionResult" has a number of derived classes like
"JsonResult", "FileStreamResult" and "ViewResult" .
■ "ActionResult" is best if you are deriving different types of view dynamically.
11. Explain what is the difference
between View and Partial View?
Sr. View Partial View
No.
1. It contains the layout page It does not contain the layout page
2. Before any view is rendered, viewstart page is Partial view does not verify for a
rendered viewstart.cshtml. We cannot put common code
for a partial view within the
viewStart.cshtml.page
3. View might have markup tags like body, html, Partial view is designed specially to render
head, title, meta etc. within the view and just because of that it does
not consist any mark up
4. View is not lightweight as compare to Partial We can pass a regular view to the RenderPartial
View method
12. What is the MVC Life Cycle?
■ 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.
■ Creating Response object: - This phase has two steps executing the action and finally sending
the response as a result to the view.
■ Step 5 Execute Action: - The "ControllerActionInvoker" determines which action to executed and
executes the action.
■ Step 6 Result sent: - The action method executes and creates the type of result which can be a
view result , file result , JSON result etc.
13. What is RouteConfig.cs in MVC?
■ “RouteConfig.cs” holds the routing configuration for MVC.
■ RouteConfig will be initialized on Application_Start event registered in Global.asax.
■ Every MVC application must configure (register) at least one route, which is
configured by MVC framework by default. You can register a route
in RouteConfig class, which is in RouteConfig.cs under App_Start folder.
14. Can you explain RenderBody and
RenderPage in MVC?
■ RenderBody is like ContentPlaceHolder in web forms. This will exist in layout page
and it will render the child pages/views. Layout page will have only one
RenderBody() method. RenderPage also exists in Layout page and multiple
RenderPage() can be there in Layout page.
15. Explain Bundle.Config in MVC?