MVC Notes
MVC Notes
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.