ASP.NET MVC, WEB API Questions and answers.
Explain MVC (Model-View-Controller)
MVC (Model-View-Controller) is an architectural software pattern that basically decouples various
"Model" is basically domain data.
"View" is user interface to render domain data.
"Controller" translates user actions into appropriate operations performed on model.
What is ASP.NET MVC
ASP.NET MVC is a web development framework from Microsoft that is based on MVC (Model-View-Controller) architectural design pattern. Microsoft has streamlined the development of MVC based applications using ASP.NET MVC framework.
Difference between ASP.NET MVC and ASP.NET WebForms?
ASP.NET Web Forms uses Page controller pattern approach for rendering layout, whereas ASP.NET MVC uses Front controller approach. In case of Page controller approach, every page has its own controller, i.e., code-behind file that processes the request. On the other hand, in ASP.NET MVC, a common controller for all pages processes the requests.
What are the Core features of ASP.NET MVC?
Core features of ASP.NET MVC framework are:
• Clear separation of application concerns (Presentation and Business Logic)
• An extensible and pluggable framework
• Extensive support for ASP.NET Routing
• Support for existing ASP.NET features
Can you please explain the request flow in ASP.NET MVC framework?
What is Routing in ASP.NET MVC?
In case of a typical ASP.NET application, incoming requests are mapped to physical files such as .aspx file. ASP.NET MVC framework uses
ASP.NET MVC framework uses a routing engine, that maps URLs to controller classes. We can define routing rules for the engine, so that it can map incoming request URLs to appropriate controller.
Practically, when a user types a URL in a browser window for an ASP.NET MVC application and presses “go” button, routing engine uses routing rules that are defined in Global.asax file in order to parse the URL and find out the path of corresponding controller.
What is the difference between ViewData, ViewBag and TempData?
In order to pass data from controller to view and in next subsequent request, ASP.NET MVC framework provides different options i.e., ViewData, ViewBag and TempData.
Both ViewBag and ViewData are used to communicate between controller and corresponding view. But this communication is only for server call, it becomes null if redirect occurs. So, in short, it's a mechanism to maintain state between controller and corresponding view.
ViewData is a dictionary object while ViewBag is a dynamic property (a new C#
TempData is also a dictionary object that stays for the time of an HTTP Request. So, Tempdata can be used to maintain data between redirects, i.e., from one controller to the other controller.
What are Action Methods in ASP.NET MVC?
The ASP.NET MVC framework maps URLs to classes that are referred to as
The base class for
Locating the appropriate action method to call and validating that it can be called.
Getting the values to use as the action method's arguments.
Handling all errors that might occur during the execution of the action method.
Providing the default
WebFormViewEngine class for rendering ASP.NET page types (views).
Explain the role of Model in ASP.NET MVC?
One of the core features of ASP.NET MVC is that it separates the input and UI logic from
Model is normally responsible for accessing data from some persistent medium like database and manipulate it.
What are Action Filters in ASP.NET MVC?
If we need to apply some specific logic before or after action methods, we use action filters. We can apply these action filters to a controller or a specific controller action. Action filters are basically custom classes that provide a means for adding pre-action or post-action behavior to controller actions.
Authorize filter can be used to restrict access to a specific user or a role.
OutputCache filter can cache the output of a controller action for a specific duration.
IN WHICH ASSEMBLY IS THE MVC FRAMEWORK DEFINED?
What is JSON Binding?
What is Dependency Resolution?
MVC 3 introduced a new concept called a dependency resolver, that greatly simplified the use of dependency injection in your applications. This made it easier to decouple application components, making them more configurable and easier to test.
Support was added
Controllers (registering and injecting controller factories, injecting controllers)
Views (registering and injecting view engines, injecting dependencies into view pages)
Action filters (locating and injecting filters)
Model validation providers (registering and injecting)
Model metadata providers (registering and injecting)
Value providers (registering and injecting)
What are HTML helpers in MVC?
HTML helpers help you to render HTML controls in the view. For instance if you want to display a HTML textbox on the view , below is the HTML helper code.
<%= Html.TextBox("LastName") %>
For checkbox below is the HTML helper code. In this way we have HTML helper methods for every HTML control that exists.
What is the difference between “HTML.TextBox” vs “HTML.TextBoxFor”?
Both of them provide the same HTML output, “HTML.TextBoxFor” is strongly typed while “HTML.TextBox” isn’t. Below is a simple HTML code which just creates a simple textbox with “CustomerCode” as name.
Below is “Html.TextBoxFor” code which creates HTML textbox using the property name ‘CustomerCode” from object “m”.
In the same way we have for other HTML controls like for checkbox we have “Html.CheckBox” and “Html.CheckBoxFor”.
What is routing in MVC?
Routing helps you to define a URL structure and map the URL with the controller.
For instance let’s say we want that when a user types “http://localhost/View/ViewCustomer/”, it goes to the “Customer” Controller and invokes the DisplayCustomer action. This is defined by adding an entry in to the routes collection using the maproute function. Below is the underlined code which shows how the URL structure and mapping with controller and action is defined.
Where is the route mapping code written?
The route mapping code is written in "RouteConfig.cs" file and registered using "global.asax" application start event.
Can we map multiple URL’s to the same action?
Yes, you can, you just need to make two entries with different key names and specify the same controller and action.
Explain attribute based routing in MVC?
This is a feature introduced in MVC 5. By using the "Route" attribute we can define the URL structure. For example in the below code we have decorated the "GotoAbout" action with the route attribute. The route attribute says that the "GotoAbout" can be invoked using the URL structure "Users/about".
What is the advantage of defining route structures in the code?
Most of the time developers code in the action methods. Developers can see the URL structure right upfront rather than going to the “routeconfig.cs” and see the lengthy codes. For instance in the below code the developer can see right upfront that the “GotoAbout” action can be invoked by four different URL structure.
This is much user friendly as compared to scrolling through the “routeconfig.cs” file and going through the length line of code to figure out which URL structure is mapped to which action.
How can we navigate from one view to another using a hyperlink?
By using the ActionLink method as shown in the below code. The below code will create a simple URL which helps to navigate to the “Home” controller and invoke the GotoHome action.
How can we restrict MVC actions to be invoked only by GET or POST?
We can decorate the MVC action with the HttpGet or HttpPost attribute to restrict the type of HTTP calls. For instance you can see in the below code snippet the DisplayCustomer action can only be invoked by HttpGet. If we try to make HTTP POST on DisplayCustomer, it will throw an error.
How can we maintain sessions in MVC?
Sessions can be maintained in MVC by three ways: tempdata, viewdata, and viewbag.
What is the difference between tempdata, viewdata, and viewbag?
Figure: Difference between tempdata, viewdata, and viewbag
Temp data - Helps to maintain data when you move from one controller to another controller or from one action to another action. In other words when you redirect, tempdata helps to maintain data between those redirects. It internally uses session variables.
View data - Helps to maintain data when you move from controller to view.
View Bag - It’s a dynamic wrapper around view data. When you use Viewbag type, casting is not required. It uses the dynamic keyword internally.
Figure: dynamic keyword
Session variables - By using session variables we can maintain data from any entity to any entity.
Hidden fields and HTML controls - Helps to maintain data from UI to controller only. So you can send data from HTML controls or hidden fields to the controller using POST or GET HTTP methods.
Below is a summary table which shows the different mechanisms for persistence.
Maintains data between
Controller to Controller
Controller to View
View to Controller
What is difference between TempData and ViewData ?
“TempData” maintains data for the complete request while “ViewData” maintains data only from Controller to the view.
Does “TempData” preserve data in the next request also?
“TempData” is available through out for the current request and in the subsequent request it’s available depending on whether “TempData” is read or not.
So if “TempData” is once read it will not be available in the subsequent request.
What is the use of Keep and Peek in “TempData”?
Once “TempData” is read in the current request it’s not available in the subsequent request. If we want “TempData” to be read and also available in the subsequent request then after reading we need to call “Keep” method as shown in the code below.
The more shortcut way of achieving the same is by using “Peek”. This function helps to read as well advices MVC to maintain “TempData” for the subsequent request.
Recommended Book for MVC : List of Some Important books for .NET Framework, C#, MVC, WCF