In ASP.NET MVC, an application follows the Model-View-Controller (MVC) design pattern, which promotes separation of concerns and helps organize code for better maintainability and scalability. An MVC application typically has a well-defined structure, which separates different components into specific folders and files for clarity.
Here’s an overview of the structure and purpose of the key components in an ASP.NET MVC application:
Example:
namespace MyApp.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
Purpose: The View is responsible for rendering the user interface (UI). Views are typically Razor files (.cshtml) that contain HTML and C# code to dynamically generate content.
Location: The Views folder.
Example:
@model MyApp.Models.Product
<h2>@Model.Name</h2>
<p>Price: @Model.Price</p>
Example:
namespace MyApp.Controllers
{
public class ProductController : Controller
{
// Action method to display a product
public ActionResult Details(int id)
{
var product = productService.GetProductById(id); // Fetch product from the service
return View(product); // Pass product data to the view
}
}
}
Example:
@* _Layout.cshtml *@
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
</head>
<body>
<header>
<h1>Welcome to MyApp</h1>
</header>
@RenderBody() <!-- Where the view content will go -->
<footer>© 2025 MyApp</footer>
</body>
</html>
Example: RouteConfig.cs inside App_Start sets up routing for the application.
namespace MyApp.App_Start
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
In the MVC pattern, Controllers are responsible for handling user input and interacting with models to fetch or update data. The controller then decides which view to display to the user and passes the data to that view.
A controller is typically a C# class that inherits from the Controller base class provided by ASP.NET MVC. Each action within the controller corresponds to a user request and returns an appropriate result (usually a view or redirect).
Example of action methods:
public class ProductController : Controller
{
// Action to show product details
public ActionResult Details(int id)
{
var product = productService.GetProductById(id); // Assume productService fetches data from a database
return View(product); // Returns the product data to the view
}
// Action to display a list of products
public ActionResult Index()
{
var products = productService.GetAllProducts();
return View(products); // Pass products to the view
}
}
In the above example:
Details(int id): Displays the details of a single product based on the provided id.Index(): Displays a list of all products.Routing determines how URL patterns map to controller actions. ASP.NET MVC uses the RouteConfig.cs file to define how URL patterns should be matched to controller actions.
For example, the URL http://example.com/Product/Details/1 would be routed to the ProductController and invoke the Details action with the id = 1.
The default routing rule in the RouteConfig.cs file is:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This means:
Product).Details).1).The ActionResult class is the base class for all results returned by action methods. Some common derived types of ActionResult are:
Example:
public ActionResult RedirectToHome()
{
return RedirectToAction("Index", "Home"); // Redirects to Home/Index
}
public JsonResult GetProductData(int id)
{
var product = productService.GetProductById(id);
return Json(product, JsonRequestBehavior.AllowGet); // Returns product data as JSON
}
Data is passed from a controller to a view in the following ways:
ViewData: A dictionary used to pass data between the controller and the view.
public ActionResult Details(int id)
{
var product = productService.GetProductById(id);
ViewData["Product"] = product; // Passing data to the view using ViewData
return View();
}
ViewBag: A dynamic object used to pass data to the view.
public ActionResult Details(int id)
{
var product = productService.GetProductById(id);
ViewBag.Product = product; // Passing data to the view using ViewBag
return View();
}
Strongly Typed Models: Pass a model directly to the view, ensuring type safety.
public ActionResult Details(int id)
{
var product = productService.GetProductById(id);
return View(product); // Passing strongly typed model to the view
}
The MVC architecture in ASP.NET MVC provides a clean separation of concerns by dividing the application into Models, Views, and Controllers. The Controllers play a crucial role in managing user requests, interacting with the models, and returning views or other results.
By using controllers effectively, you can keep the logic of processing requests separate from the UI, making the application more maintainable, testable, and scalable.
Open this section to load past papers