ASP.NET MVC (Model-View-Controller) is a web development framework from Microsoft that provides a powerful, flexible way to build web applications. It's part of the broader ASP.NET family and is used to create web applications that follow the MVC design pattern. The main goal of ASP.NET MVC is to separate an application into three interconnected components: Model, View, and Controller. This helps in organizing code, promoting clean separation of concerns, and making the application more maintainable.
Let’s explore ASP.NET MVC in more detail.
The Model-View-Controller (MVC) design pattern divides an application into three main components:
When a user makes a request in an ASP.NET MVC application, the following steps occur:
Example of a Model Class:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
In this example, the Product model has properties that define the data (Id, Name, Price) of a product.
Example of a Razor View (Product View):
@model MyApp.Models.Product
<h2>@Model.Name</h2>
<p>Price: @Model.Price</p>
In this example:
@model directive indicates that the view is strongly typed to the Product model.@Model.Name and @Model.Price output the name and price of the product.Example of a Controller Class:
public class ProductController : Controller
{
// Action method to display a product
public ActionResult Details(int id)
{
var product = productService.GetProductById(id); // Assume productService fetches data from a database
return View(product); // Passing product model to the view
}
}
In this example:
Details action method takes an id parameter, retrieves the corresponding product from the database, and returns a view displaying the product details.Routing is a core feature of ASP.NET MVC. It is responsible for mapping incoming URLs to specific controller actions. Routing rules are defined in the RouteConfig.cs file, typically found in the App_Start folder.
Example of Routing Configuration:
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 this example:
{controller}/{action}/{id} tells ASP.NET MVC that the first part of the URL corresponds to the controller, the second part to the action, and the third part to the optional id.For example, if the user accesses the URL /Product/Details/1, the ProductController's Details action method will be invoked with the id = 1.
When a controller action is executed, it returns an ActionResult. The ActionResult represents the result of the action method, which can be of different types depending on what is returned.
Example of Returning a View:
public ActionResult Index()
{
var products = productService.GetAllProducts(); // Fetch products from the model/service
return View(products); // Return a view, passing the list of products
}
In this example:
ASP.NET MVC is a robust, powerful framework for building dynamic, scalable web applications using the Model-View-Controller (MVC) design pattern. The separation of concerns provided by the MVC architecture helps in organizing code, ensuring maintainability, and promoting testability. With Razor views, strong routing, and integration with Entity Framework for data handling, ASP.NET MVC is a popular choice for developers looking to build web applications with clean, organized code.
Open this section to load past papers