Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

MVC Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

ASP.

NET MVC Notes


Q. Request Life cycle of MVC?
Ans: when the client made the first request, then the Application_Start()
method of the Global.asax.cs class file is going to be fired. this is the first
method that is executed when the application starts. The Application_Start
event is used to set up the initial required configuration needed in order to run
the application such as Registering the Routes. the Application_Start event
calls the RegisterRoutes Method of the RouteConfig class, then the
RegisterRoutes method fills the Route Table with the routes defined for your
application. once the Application_End event is fired, then the next request
coming to the application will be treated as the first request and again the
Application_Start event is fired and all the above are going to happen in order.
Q. Important Files and folders Structure ?
Ans:
App_Data: The App_Data folder of an ASP.NET MVC application contains
application-related data files like .mdf files, LocalDB, and XML files, etc.
App_Start: The App_Start folder of an ASP.NET MVC application contains
configuration-related class files which are needed to be executed at the time of
application starts.
Global.asax: The Global.asax file in an ASP.NET MVC application allows us to
write the code that we want to run at the application level or you can say
global level, such as Application_BeginRequest, Application_Error,
Application_Start, Session_Start, Session_End, etc.
Web.config: The Web.config file of an ASP.NET MVC application is one of the
most useful and important files which contains the application-level
configurations such as connection strings, global variables, etc.
Q. Difference and Similarities between ViewData and ViewBag in ASP.NET
MVC?
Ans:
1. The ViewData is a dictionary object whereas the ViewBag is a dynamic
property.
2. In ViewData, we use the string as the key to store and retrieve the data
whereas in ViewBag we use the dynamic properties to store and retrieve
data.
3. The ViewData requires typecasting for complex data types and also
checks for null values to avoid any exceptions whereas ViewBag doesn’t
require any typecasting for the complex data type.
Q. Strongly Typed Views in ASP.NET MVC Application?
Ans: In order to create a strongly typed view in ASP.NET MVC application, we
need to pass the model object as a parameter to the View() extension method
and we need to specify the model type within the view by using the @model
directive Then we can access the model properties simply by using @Model,
where the letter M will be in uppercase.
Q. What is a ViewModel in ASP.NET MVC?
Ans: In an ASP.NET MVC application, a single model object may not contain all
the necessary data required for a view. For example, a view may require
different model data. Then in such situations like this, we need to use the
concept ViewModel. A ViewModel in ASP.NET MVC application is a model
which contains more than one model data required for a particular view. As
this model is specific for a particular view, we call this ViewModel.
Q. Why do we need TempData in the ASP.NET MVC Application?
Ans: The limitation of both ViewData and ViewBag is they are limited to one
HTTP request only. So, if redirection occurs then their values become null
means they will lose the data they hold. In many real-time scenarios, we may
need to pass the data from one HTTP Request to the next subsequent HTTP
Request. Then in such situations like this, we need to use TempData.
Q. How to retain TempData values in the consecutive request?
Ans: In order to retain the TempData value in the third consecutive request,
we need to call TempData.Keep() method.

ROUTING QUESTIONS
Q. What is Routing in ASP.NET MVC?
Ans: The ASP.NET MVC Routing module is responsible for mapping the
incoming browser requests (i.e. the incoming URL or incoming HTTP Requests)
to a particular controller action method. This mapping is done by the routing
rules defined for your application.
Q. How to configure routes?
Ans: if we want to configure any routes then we need to configure the routes
within the RegisterRoute method of RouteConfig class using the MapRoute
extension method. While configuring the Routes, at least two parameters we
need to provide to the MapRoute method i.e. Route name and URL pattern.
The Default parameter is optional.
Q. What are Route Constraints in ASP.NET MVC Application?
Ans: The Route Constraint in ASP.NET MVC Routing allows us to apply a regular
expression to a URL segment to restrict whether the route will match the
request. In simple words, we can say that the Route constraint is a way to put
some validation around the defined route.
Q. What is URL rewriting?
Ans: URL rewriting is the process of intercepting an incoming Web request and
redirecting the request to a different resource. When performing URL
rewriting, typically the URL being requested is checked and, based on its value,
the request is redirected to a different URL.
Q. What is the difference between Routing and URL Rewriting?
Ans:
1. URL rewriting is focused on mapping one URL (new URL) to another URL
(old URL) while routing is focused on mapping a URL to a resource i.e.
controller action method.
2. URL rewriting rewrites your old URL to a new one while routing never
rewrites your old URL to a new one but it maps to the original route.
Q. What is Attribute Routing in ASP.NET MVC?
Ans: If we are defining Routes by using the [Route] attribute is called Attribute
Routing. It provides you more control over the URIs by defining routes directly
on actions and controllers.
Q. When to use ASP.NET MVC Attribute Routing?
Ans: In some scenarios, convention-based routing is very difficult to support
certain URL patterns. But those URL patterns can be easily achieved by using
the Attribute Routing For example, resources often contain child resources like
Clients have Ordered; Movies have Actors, and Books have Authors, and so on.
It’s natural to create URIs that reflects these relation.
Ex Client/1/orders
Q. How to enable Attribute Routing in ASP.NET MVC?
Ans: You can easily enable the attribute routing just by making a call to the
routes.MapMvcAttributeRoutes() method within RegisterRoutes() method of
RouteConfig file.
Q. What is the use of the RoutePrefix attribute?
Ans: The RoutePrefix attribute is used to specify the common route prefix at
the controller level which will eliminate the need to repeat that common route
prefix on each and every action method of the controller.
Q. How to override the route prefix?
Ans: Use ~ character to override the route prefix.
Q. Route Constraints in ASP.NET MVC Attribute Routing?
Ans: Route Constraints in ASP.NET MVC Attribute Routing are nothing but a set
of rules that can be applied to the route parameters. By using the “:” symbol
we can applied Route Constraints to the Route Parameters.
Q. What is Model Binding in ASP.NET MVC?
Ans: Model Binding is a mechanism that maps the HTTP request data with a
model. It is the process of creating .NET objects using the data sent by the
browser in an HTTP request.
Q. What are HTML Helpers in ASP.NET MVC?
Ans: An HTML Helper is an extension method of the HTML Helper class which
is used to generate HTML content in a view.
Q. What are the Differences between Html.TextBox and Html.TextBoxFor in
ASP.NET MVC application?
Ans: The Html.TextBox() Helper method is not strongly typed and hence they
don’t require a strongly typed view. This means that we can hardcode
whatever name we want. On the other hand the Html.TextBoxFor() HTML
Helper method is a strongly typed method and hence it requires a strongly
typed view and the name should be given using the lambda expression.
Q. What are Data Annotations in ASP.NET MVC?
Ans: Data Annotations help us to define the rules to the model classes or
properties for data validation and displaying suitable messages to end-users.
Data Annotation attribute classes are present in
System.ComponentModel.DataAnnotations.

Q. Built-in Data Annotation Validator Attributes in ASP.NET MVC ?


Ans:
1. DataType – Specify the datatype of a property
2. DisplayName – specify the display name for a property.
3. DisplayFormat – specify the display format for a property like the
different format for a Date property.
4. Required – Specify a property as required.
5. regular expression – validate the value of a property by the specified
regular expression pattern.
6. Range – validate the value of a property within a specified range of
values.
7. StringLength – specify min and max length for a string property
8. MaxLength – specify max length for a string property.
9. Bind – specify fields to include or exclude when adding parameter or
form values to model properties.
10.ScaffoldColumn – specify fields for hiding from editor forms.
Q. ValidationSummary Attribute in ASP.NET MVC Application?
Ans: The ValidationSummary helper method generates an unordered list (ul
element) of validation messages that are in the ModelStateDictionary object.
The ValidationSummary can be used to display all the error messages for all
the fields. It can also be used to display custom error messages.
Q. What is the Action Method in ASP.NET MVC?
Ans: All the public methods inside a Controller which respond to the URL are
known as Action Methods. When creating an Action Method, we must follow
the some rules.
1. The action method must be public.
2. It cannot be overloaded
3. It cannot be a static method
4. ActionResult is the base class of all the result types that an action
method returns.
Q. What is the Action Result in ASP.NET MVC?
Ans: Action Result is the return type of an action method. The action result is
an abstract class. It is the base class for all types that an action method returns
like View, Partial View, Redirect, Json, Content, File, Redirect To Action, etc. are
derived from the abstract Action Result class and these types can also be used
as the return type of an action method.
Q. Types of Action Results?
Ans:
1. ViewResult – Represents HTML and markup.
2. PartialViewResult – Represents HTML and markup.
3. EmptyResult – Represents no result.
4. RedirectResult – Represents a redirection to a new URL.
5. RedirectToActionResult – It is returning the result to a specified
controller and action method
6. JsonResult – Represents a JavaScript Object Notation result that can be
used in an AJAX application.
7. JavaScriptResult – Represents a JavaScript script.
8. ContentResult – Represents a text result.
9. FileContentResult – Represents a downloadable file (with the binary
content).
10.FilePathResult – Represents a downloadable file (with a path).
11.FileStreamResult – Represents a downloadable file (with a file stream).
Q. What are Partial Views in ASP.NET MVC and it’s needed?
Ans: When we need a common part of the user interface at multiple pages in a
web application then we develop a partial view. Hence partial view is a regular
view that can be used multiple times in an application and has the file
extension .cshtml.
Q. What are the Layouts in ASP.NET MVC?
Ans: Layouts are used to maintain a consistent look and feel across multiple
views within the application. As compared to Web Forms, layouts serve the
same purpose as master pages but offer a simple syntax and greater flexibility.
Q. What are Sections in ASP.NET MVC?
Ans: A section allows us to specify a region of content within a layout. It
expects one parameter which is the name of the section. If you don’t provide
that, an exception will be thrown.
Q. What are RenderBody and RenderPage in ASP.NET MVC?
Ans: RenderBody method exists in the Layout page to render child page/view.
It is just like the ContentPlaceHolder on the master page. A layout page can
have only one RenderBody method.
The reader page method also exists in the Layout page to render another page
that exists in your application. A layout page can have multiple RenderPage
methods.
Q. What is _ViewStart?
Ans: The _ViewStart.cshml page is used to serve a common layout page(s) for a
group of views. The code within this file is executed before the code in any
view placed in the same directory.
Q. Session in ASP.NET MVC?
Ans: The session is used to pass data within the application and Unlike
TempData, it persists data for a user session until it is timeout (by default
session timeout is 20 minutes).A session is valid for all requests, not for a single
redirect. It’s also required typecasting for getting data and check for null values
to avoid the error.
Q. What is a View Engine in ASP.NET MVC application?
Ans: View Engine is responsible for converting the Views into HTML markup
and then rendering the HTML in a browser.
Q. What is Razor View Engine?
Ans: The Razor Engine was introduced in ASP.NET MVC3. This is an advanced
View Engine that provides a new way to write markup syntax which will reduce
the typing of writing the complete HTML tag. the Razor syntax is easy to learn
and much cleaner than Web Form syntax. Razor uses @ symbol to write
markup.
Q. What is the Output Caching in ASP.NET MVC Application?
Ans: The OutputCache filter allows you to cache the data that is the output of
an action method. By default, this attribute filter caches the data until 60
seconds. After 60 sec, ASP.NET MVC will execute the action method again and
cache the output again.

Q. What are Filters in ASP.NET MVC Application?


Ans: The Filters are the attribute that allows us to inject some logic or code
which is going to be executed either before or after an action method is
invoked.
Q. Why do we need to use Filters in the ASP.NET MVC Applications?
Ans: Basically, Filters are used to perform the following common
functionalities in your application
1. Caching
2. Logging
3. Error Handling
4. Authentication and Authorization, etc.
Q. What are the Different Types of Filters available in ASP.NET MVC
Framework?
Ans: The ASP.NET MVC 5 framework provides five different types of Filters.
They are as follows
1. Authentication Filter (Introduced in MVC 5): The Authentication filter is
the first filter that is going to be executed before executing any other
filter or action method. This filter checks that the user from where the
request is coming is a valid user or not.
2. Authorization Filter: The Authorization Filters are executed after the
Authentication Filter. This filter is used to check whether the user has
the right to access a particular resource or page
3. Action Filter: The Action Filters will be executed before the action
method starts executing or after the action has been executed. So, if you
want to execute some custom logic that is going to be executed before
or after an action method is executed, then you need to use the Action
Filters in applications.
4. Result Filter: The Result filters are executed before or after generating
the result for an action.
5. Exception Filter : The Exception filters are executed when there is an
unhandled exception occurs during either the execution of actions or
filters
Q. What is Exception Filter in ASP.NET MVC Application?
Ans: The Exception Filter is used to handle any exceptions that occur during
the Request processing pipeline. The ASP.NET MVC Framework provides one
in-built attribute called HandleError which is basically used to handle the
unhandled exception in the MVC application.

You might also like