0% found this document useful (0 votes)
48 views21 pages

MVC PDF

MVC is a software architecture pattern that separates an application into three main components: the model, the view, and the controller. The model manages the application's data logic, the view displays the user interface, and the controller interprets user input and updates the model and view accordingly. Some key advantages of MVC include separating a project into distinct segments to improve development and maintenance, and making projects more systematic and organized.

Uploaded by

Snehasish Patra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views21 pages

MVC PDF

MVC is a software architecture pattern that separates an application into three main components: the model, the view, and the controller. The model manages the application's data logic, the view displays the user interface, and the controller interprets user input and updates the model and view accordingly. Some key advantages of MVC include separating a project into distinct segments to improve development and maintenance, and making projects more systematic and organized.

Uploaded by

Snehasish Patra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

ASP.

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?

■ MVC segregates your project into a different segment, and it becomes


easy for developers to work on
■ It is easy to edit or change some part of your project that makes
project less development and maintenance cost
■ MVC makes your project more systematic
3. What is Razor View Engine?
■ Razor View engine is a markup syntax which helps us to write HTML and server-side
code in web pages using C# or VB.NET. It is server-side markup language however it
is not at all a programming language.
■ Razor is a templating engine and ASP.NET MVC has implemented a view engine
which allows us to use Razor inside of an MVC application to produce HTML.
However, Razor does not have any ties with ASP.NET MVC.
Some of Razor Syntax Rules for C# are given below.
■ It must be always enclosed in @{ ... }
■ Semicolon “;” must be used to ending statements
■ Files have .cshtml extension.
■ Variables are declared with var keyword
■ Inline expressions (variables and functions) start with @
■ C# code is case sensitive
4. What you mean by Routing in MVC?
■ ASP.NET MVC routing is a pattern matching system that is responsible for mapping
incoming browser requests to specified MVC controller actions. When the ASP.NET
MVC application launches then the application registers one or more patterns with
the framework's route table to tell the routing engine what to do with any requests
that matches those patterns. When the routing engine receives a request at
runtime, it matches that request's URL against the URL patterns registered with it
and gives the response according to a pattern match.

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?

■ “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.
16. 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.
■ Eg : 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.
17. What is Bundling and Minification in
MVC?
■ Bundling and minification are two new techniques introduced to improve request
load time. It improves load time by reducing the number of requests to the server
and reducing the size of requested assets (such as CSS and JavaScript).
■ Bundling: It lets us combine multiple JavaScript (.js) files or multiple cascading style
sheet (.css) files so that they can be downloaded as a unit, rather than making
individual HTTP requests.
■ Minification: It extracts the whitespace and performs other types of compression to
make the downloaded files as small as possible. At runtime, the process recognizes
the agent of the user, for example, IE, Mozilla, etc. and then removes whatever is
specific to Mozilla when the request comes from IE.
18. What is difference between MVC and Web Forms?
ASP.NET MVC ASP.NET Web Forms
1. View and logic are separate, it has separation of No separation of concerns; Views are tightly coupled with
concerns theory. MVC 3 onwards has .aspx page as logic (.aspx.cs /.vb file).
.cshtml.
2. Introduced concept of routing for route based URL. File-based routing .Redirection is based on pages.
Routing is declared in Global.asax for example.
3. Support Razor syntax as well as .aspx Support web forms syntax only.
4. State management handled via Tempdata, ViewBag, State management handled via View State. Large viewstate,
and View Data. Since the controller and view are not in other words increase in page size.
dependent and also since there is no view state
concept in ASP.NET, MVC keeps the pages lightweight.
5. Multiple pages can have the same controller to satisfy Each page has its own code, in other words direct
their requirements. A controller may have multiple dependency on code. For example Sachin.aspx is dependent
Actions (method name inside the controller class). on Sachin.aspx.cs (code behind) file.
6. Unit Testing is quite easier than ASP.Net Web forms Direct dependency, tight coupling raises issues in testing.
Since a web form and code are separate files
7. layouts Master pages
8. Partial Views User Controls
19. How can we pass the data From
Controller To View In MVC?
■ There are three options in Model View Controller (MVC) for passing data from controller to view.
This article attempts to explain the differences among ViewData, ViewBag and TempData with
examples. ViewData and ViewBag are similar and TempData performs additional responsibility.
The following are the key points on those three objects.
ViewBag
ViewData
TempData
■ ViewData moves data from controller to view.
■ Use TempData when you need data to be available for the next request, only. In the next request,
it will be there but will be gone after that.
■ TempData is used to pass data from the current request to the subsequent request, in other
words in case of redirection. That means the value of TempData will not be null.
20. What is Data Annotation Validator Attributes in MVC?
■ DataAnnotation plays a vital role in added validation to properties while designing the model itself. This validation can be
added for both the client side and the server side.
You understand that decorating the properties in a model with an Attribute can make that property eligible for Validation.
Some of the DataAnnotation used for validation are given below,
■ Required
Specify a property as required.
1. [Required(ErrorMessage="CustomerName is mandatory")]
■ RegularExpression
Specifies the regular expression to validate the value of the property.
1. [RegularExpression("[a-z]", ErrorMessage = "Invalid character")]
■ Range
Specifies the Range of values between which the property values are checked.
1. [Range(1000,10000,ErrorMessage="Range should be between 1k & 10k")]
■ StringLength
Specifies the Min & Max length for a string property.
1. [StringLength(50, MinimumLength = 5, ErrorMessage = "Minimum char is 5 and maximum char is 10")]
■ MaxLength
Specifies the Max length for the property value.
1. [MaxLength(10,ErrorMessage="Customer Code is exceeding")]
■ MinLength
It is used to check for minimum length.
1. [MinLength(5, ErrorMessage = "Customer Code is too small")]

You might also like