General MVC Concepts
What is Model-View-Controller?
- Model-View-Controller (MVC) is a software architectural pattern that separates an application into three main logical components: the Model, the View, and the Controller. Each of these components is responsible for handling different aspects of the application.
What does Model-View-Controller represent in an MVC application?
- Model: Represents the data and business logic. It directly manages the data, logic, and rules of the application.
- View: Represents the user interface. It displays the data from the model to the user and sends user commands to the controller.
- Controller: Handles user input, manipulates the model, and updates the view as needed. It acts as an intermediary between the model and the view.
Name the Assembly to Define MVC
- The ASP.NET MVC framework is defined in the
System.Web.Mvc
assembly.
- The ASP.NET MVC framework is defined in the
Controllers and Routing
What are the Different Return Types of a Controller Action Method?
ViewResult
: Returns a view.PartialViewResult
: Returns a partial view.JsonResult
: Returns JSON-formatted data.RedirectResult
: Redirects to a new URL.RedirectToRouteResult
: Redirects to a specified route.ContentResult
: Returns plain text content.FileResult
: Returns a file for download.EmptyResult
: Returns no result (used to return nothing).
What is the Difference Between Adding Routes to a WebForm Application and an MVC Application?
- WebForm Application: Routing is less commonly used, and requests are usually mapped directly to file paths.
- MVC Application: Routing is central, mapping URL patterns to specific controller actions using route definitions in
RouteConfig.cs
.
What are the Two Ways to Add Constraints to a Route?
- Inline Constraints: Directly specifying constraints in the route URL pattern, e.g.,
{id:int}
. - Custom Constraints: Using a custom class that implements
IRouteConstraint
to define more complex constraints.
- Inline Constraints: Directly specifying constraints in the route URL pattern, e.g.,
MVC Features and Benefits
What are the Advantages of MVC?
- Separation of Concerns: Divides application logic into three interconnected components, making the application more modular and maintainable.
- Testability: Easy to test each component independently.
- Scalability: Suitable for large-scale applications due to its modularity.
- Flexibility: Supports multiple views for a model, promoting reusability.
What “beforeFilter()”, “beforeRender()”, and “afterFilter()” Functions Do in Controller?
- These functions are common in frameworks like CakePHP:
- beforeFilter(): Executes code before any action in the controller.
- beforeRender(): Executes code before rendering the view.
- afterFilter(): Executes code after the controller action and view have been rendered.
- These functions are common in frameworks like CakePHP:
What is the Role of Components Presentation, Abstraction, and Control in MVC?
- Presentation: Handles displaying the UI to the user (View).
- Abstraction: Manages the data and business logic (Model).
- Control: Acts as a bridge between the model and the view, handling user input and updating the model/view (Controller).
What are the Drawbacks of the MVC Model?
- Complexity: MVC can introduce complexity for small applications.
- Learning Curve: Developers need to understand the MVC structure, which can take time.
- Overhead: Can lead to over-separation of logic in small applications, making it unnecessarily complex.
Action Filters, Routing, and Execution
What is the Role of “ActionFilters” in MVC?
- Action filters are used to execute code before or after an action method executes. Examples include authentication checks, logging, or error handling.
What are the Steps for the Execution of an MVC Project?
- Step 1: Configure routing in
RouteConfig.cs
. - Step 2: Instantiate the controller based on the route.
- Step 3: Invoke the action method.
- Step 4: Process and execute any filters.
- Step 5: Generate the result (e.g., view or JSON).
- Step 6: Render the view and send the response to the client.
- Step 1: Configure routing in
What is Routing and Three Segments?
- Routing is the process of mapping URLs to specific controller actions.
- The three segments in routing typically represent:
- Controller: Name of the controller.
- Action: Name of the action method.
- Parameters: Additional parameters for the action method.
How Routing is Done in the MVC Pattern?
- Routes are defined in a route configuration file (e.g.,
RouteConfig.cs
), mapping URL patterns to controller actions usingMapRoute()
method.
- Routes are defined in a route configuration file (e.g.,
View Navigation and Session Management
How Can You Navigate from One View to Another View Using a Hyperlink?
- Use the
Html.ActionLink
helper method to create hyperlinks between views:1
@Html.ActionLink("Go to About Page", "About", "Home")
- Use the
How are Sessions Maintained in MVC?
- Sessions can be maintained using the
Session
object, which stores key-value pairs, or by using cookies, hidden fields, or query strings for more secure or customized session management.
- Sessions can be maintained using the
Data Passing Between Components
What is the Difference Between TempData, ViewData, and ViewBag?
- TempData: Stores data temporarily and is available for the next request only.
- ViewData: A dictionary object to pass data from the controller to the view within the same request.
- ViewBag: A dynamic property wrapper around
ViewData
to pass data from the controller to the view within the same request.
What is a Partial View in MVC?
- A partial view is a reusable component of a view, like a user control in traditional ASP.NET. It renders a part of the HTML and can be included in multiple views.
AJAX and Result Types
How Can You Implement Ajax in MVC?
- Use jQuery
$.ajax()
for custom AJAX calls. - Use
Ajax.BeginForm()
for form submissions. - Use
@Ajax.ActionLink()
for asynchronous hyperlink calls.
- Use jQuery
What is the Difference Between “ActionResult” and “ViewResult”?
- ActionResult: A base class for all action results. It allows returning different result types like views, JSON, redirects, etc.
- ViewResult: A subclass of
ActionResult
specifically for rendering views.
How Can You Send the Result Back in JSON Format in MVC?
- Use the
JsonResult
class by returningJson(object)
from the action method.1
return Json(new { name = "John", age = 30 });
- Use the
What is the Difference Between View and Partial View?
- View: Represents a full HTML page with layout.
- Partial View: Represents a part of the page, used to render reusable sections of the UI.
Additional MVC Concepts
What are the Types of Results in MVC?
ViewResult
,PartialViewResult
,JsonResult
,RedirectResult
,RedirectToRouteResult
,ContentResult
,FileResult
,EmptyResult
.
What is the Importance of NonActionAttribute?
- The
NonAction
attribute prevents a public method in a controller from being treated as an action method, marking it as non-action.
- The
What is the Use of the Default Route
{resource}.axd/{*pathinfo}
?- This route prevents requests for web resources such as WebResource.axd or ScriptResource.axd from being passed to controllers, as these are handled by the system.
What is the Order of the Filters that Get Executed, if Multiple Filters are Implemented?
- Filters execute in this order:
- Authorization filters
- Action filters
- Result filters
- Exception filters
- Filters execute in this order:
What ASP.NET Filters are Executed in the End?
- Exception filters are executed last to handle any exceptions that occur during the action execution or result rendering.
What are the File Extensions for Razor Views?
.cshtml
for C# Razor views..vbhtml
for VB.NET Razor views.
What are the Two Ways for Adding Constraints to a Route?
- Inline Constraints in the route definition, e.g.,
{id:int}
. - Custom Constraint Classes implementing
IRouteConstraint
.
- Inline Constraints in the route definition, e.g.,
What are Two Instances Where Routing is Not Implemented or Required?
- Static File Requests: Direct requests for static files (e.g., images, CSS) don’t need routing.
- WebForm Pages: In hybrid projects with WebForm pages, those pages don’t follow MVC routing.
Features, Real-Life Examples, and Comparisons
- What are the Features of MVC?
- Separation of Concerns, Modularity, Testability, Pluggable Framework, Extensibility, Support for Multiple View Engines (R
azor, ASPX).
What are Real-Life Examples of MVC?
- E-commerce websites: Separate models for products, views for product listings, and controllers to manage user requests and responses.
- Blog platforms: Using MVC to separate content management (models) from the front-end design (views) and logic (controllers).
What is the Difference Between 3-tier Architecture and MVC Architecture?
- 3-tier Architecture: Divides application into Presentation, Business Logic, and Data Access layers.
- MVC Architecture: Focuses on separating application logic from the user interface, with a more refined interaction between its components.
How Can You Use MVC Architecture in JSP?
- Use JSP for the view layer, JavaBeans for the model, and servlets for the controller to manage user input and application flow.
MVC in Other Frameworks
How MVC Works in Spring?
- In Spring MVC, a controller processes incoming requests, interacts with the model, and returns a view to be rendered. Spring provides built-in support for dependency injection, making it easier to manage components.
What are the Important Points to Remember While Creating an MVC Application?
- Properly configure routing.
- Ensure clear separation between models, views, and controllers.
- Use action filters for cross-cutting concerns like logging or authorization.
- Utilize partial views for reusability.
- Maintain session and data flow effectively.
What is the Difference Between Web Forms and MVC?
- Web Forms: Event-driven, uses a code-behind model, and relies on postbacks and view state.
- MVC: Follows the MVC pattern, uses routing for URL management, and does not rely on view state, offering more control over HTML and client-side behavior.
How Can You Display Something in CodeIgniter?
- Load a view using the
load->view()
method in a controller:1
$this->load->view('welcome_message');
- Load a view using the
Write a Code to Demonstrate Model, View, and Controller in CodeIgniter.
- Model:
application/models/User_model.php
1 2 3 4 5
class User_model extends CI_Model { public function get_users() { return $this->db->get('users')->result_array(); } }
- Controller:
application/controllers/User.php
1 2 3 4 5 6 7
class User extends CI_Controller { public function index() { $this->load->model('User_model'); $data['users'] = $this->User_model->get_users(); $this->load->view('user_view', $data); } }
- View:
application/views/user_view.php
1 2 3
<?php foreach($users as $user): ?> <p><?php echo $user['name']; ?></p> <?php endforeach; ?>
- Model:
Mixing Web Forms and MVC
Can You Create a Web Application with Both WebForms and MVC?
- Yes, you can create a hybrid application that includes both WebForms and MVC. Use routing to separate the requests meant for WebForms from those meant for MVC.
How Can You Assign an Alias Name for ASP.NET Web API Action?
- Use the
ActionName
attribute:1 2 3 4
[ActionName("GetById")] public IHttpActionResult Get(int id) { // Action method logic }
- Use the
Differences and Use Cases
What is the Main Difference Between MVC and WebAPI?
- MVC: Designed for creating web applications with views and HTML.
- WebAPI: Designed for building RESTful services that return data (like JSON or XML).
How Can You Ensure that Web API Returns JSON Data Only?
- Configure the
GlobalConfiguration
to remove the XML formatter:1
config.Formatters.Remove(config.Formatters.XmlFormatter);
- Configure the
MVVM and AngularJS
What is the Difference Between MVVM and MVC?
- MVVM (Model-View-ViewModel): Designed for two-way data binding between the view and viewmodel, making it suitable for data-driven UIs.
- MVC: Focuses on a clear separation between UI, business logic, and data, suitable for structured applications.
What is MVC in AngularJS?
- In AngularJS, MVC refers to the way Angular handles data binding (Model), templates (View), and controller (Controller) components.
What is the Role of MVC in AngularJS?
- AngularJS utilizes a form of MVC for structuring client-side applications. The model represents the data, the view is the HTML template, and the controller manages the logic and data flow.
How to Build a Basic Controller in AngularJS?
- Define a module and a controller:
1 2 3 4
var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.message = 'Hello, World!'; });
- Define a module and a controller:
What is the Use of ng-controller in External Files in AngularJS?
ng-controller
is used to bind a specific section of the HTML to a controller, allowing dynamic content manipulation using AngularJS.
Write Code to Define Methods in AngularJS Controller.
1 2 3 4 5 6
var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.greet = function(name) { return 'Hello, ' + name + '!'; }; });
Write Code Using ng-model to Display Multi-Line Input Control in AngularJS.
1 2
<textarea ng-model="userInput"></textarea> <p>{{ userInput }}</p>
Write Code to Demonstrate the Use of Input Elements.
1 2
<input type="text" ng-model="userName" placeholder="Enter your name"> <p>Hello, {{ userName }}</p>