# Session 0: Controllers, Routing, and Return Types ## πŸ“ Overview In this session, we’ll cover the following concepts: - MVC pattern in ASP.NET Core - Controllers and their structure - Default and attribute-based routing - Handling various HTTP requests (GET, POST, DELETE) - Return types from controllers - Building a real-world Product flow - Mini project assignment ## πŸ“š Topics Covered ### βœ… MVC Architecture > Learn how the Model-View-Controller (MVC) architecture separates concerns in a web application. > πŸ”— [Microsoft Docs - MVC Pattern](https://learn.microsoft.com/en-us/aspnet/core/mvc/overview) ### βœ… Controllers and Actions > Understand how to create controllers and action methods, follow naming conventions, and return data to views. > πŸ”— [w3schools - MVC Controllers](https://www.w3schools.com/asp/asp_net_mvc_intro.asp) ### βœ… Routing in ASP.NET Core > Understand how ASP.NET Core maps incoming requests to the appropriate controller and action using default and custom routing. > πŸ”— [Microsoft Docs - Routing](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing) ### βœ… HTTP Methods in MVC > Learn how to respond to different types of HTTP requests using attributes like `[HttpGet]`, `[HttpPost]`. > πŸ”— [HTTP Methods - MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) ### βœ… Return Types > Explore different return types like `ViewResult`, `JsonResult`, and more, and know when to use each one. > πŸ”— [Action Results in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/actions) ## πŸ“Œ Notes > _Collected from various sources including W3Schools, Microsoft Docs, and ChatGPT_ --- ### **1. Start with the Basics: Understanding MVC Architecture** **Goal**: Give a quick overview of the MVC pattern, focusing on the role of controllers. - βœ… **Explain MVC**: - **Model**: Manages data and business logic. - **View**: Handles UI. - **Controller**: Acts as the intermediary, processing requests and returning responses. - βœ… **Controller’s Role**: Handles incoming HTTP requests, decides which logic to execute, and returns the appropriate result (usually a view or data). - βœ… **Activity**: Create a β€œHello World” ASP.NET Core MVC app and show the default routing behavior (`HomeController` with `Index` action). --- ### **2. Controllers: Structure, Naming, and Basics** **Goal**: Help students understand how to structure and create controllers, and introduce them to actions. - βœ… **Creating a Simple Controller**: - Use Visual Studio to add a new controller. - Controller classes must: - Be `public` - Inherit from `Controller` - Have `Controller` as a suffix in their name (e.g., `ProductController`). - βœ… **Basic Action Methods**: - Action methods must be `public`. - Default return type is usually `IActionResult`. ````csharp public class ProductController : Controller { public IActionResult Index() { return View(); } public IActionResult Details(int id) { var product = new Product { Id = id, Name = "Sample Product", Price = 25.00m }; return View(product); } } - βœ… **Activity**: Create your own `CustomerController` with an `Index` and a `Details` action. --- ### **3. Routing Basics: Default Routing and Attribute-Based Routing** **Goal**: Teach students how routing works in ASP.NET Core, from default routing to custom attribute routing. - βœ… **Default Route Configuration**: Defined in `Program.cs`: ```csharp app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); ```` - `{controller=Home}`: Default controller - `{action=Index}`: Default action - `{id?}`: Optional parameter - βœ… **Customizing Routes**: - You can modify the pattern in `Program.cs`. - Example: Use `"{controller=Product}/{action=List}/{id?}"` to change defaults. - βœ… **Attribute-Based Routing**: - Useful for APIs or when you want specific paths. ```csharp [Route("product")] public class ProductController : Controller { [HttpGet("list")] public IActionResult List() { return View(); } [HttpGet("{id:int}")] public IActionResult Details(int id) { return View(); } } ``` - πŸ”² **Activity**: Add routes to your `CustomerController`: - `[HttpGet("all")]` to show all customers - `[HttpGet("{id}")]` to show customer details --- ### **4. Controllers and Actions: Handling Different HTTP Requests** **Goal**: Show how controllers handle various HTTP methods. - βœ… **HTTP Verbs**: - **GET**: Retrieve data - **POST**: Submit data - **PUT**: Update data - **DELETE**: Remove data - βœ… **HTTP-Specific Attributes**: - Use `[HttpGet]`, `[HttpPost]`, `[HttpPut]`, `[HttpDelete]` to restrict access. ```csharp public class ProductController : Controller { [HttpGet] public IActionResult List() { return View(); } [HttpPost] public IActionResult Create(Product product) { // Save to DB return RedirectToAction("List"); } [HttpDelete("{id}")] public IActionResult Delete(int id) { // Remove from DB return NoContent(); } } ``` - πŸ”² **Activity**: Add full CRUD actions to `CustomerController`, using appropriate HTTP verbs and routing. --- ### **5. Return Types: Exploring Different Return Options in Controllers** **Goal**: Familiarize students with different return types in ASP.NET Core. - βœ… **Basic Return Types**: - `ViewResult` β†’ `return View();` - `JsonResult` β†’ `return Json(data);` - `ContentResult` β†’ `return Content("Text");` - βœ… **When to Use**: - Use `View()` when rendering pages. - Use `Json()` for APIs or AJAX. - Use `Content()` for simple text/plain responses. ```csharp public class ProductController : Controller { public IActionResult Index() => View(); public JsonResult GetProductJson(int id) => Json(new { id, name = "Product" }); public ContentResult GetMessage() => Content("This is a simple text message."); } ``` - βœ… **Advanced Return Types**: - `RedirectToAction("Index")`: Navigate to another action. - `StatusCode(404)`: Return HTTP status codes. - `File()`: Return downloadable content (e.g., PDF, image). - πŸ”² **Activity**: Add: - One action returning JSON - One returning text - One redirecting to another action --- ### **6. Real-World Application: Creating a Full Flow** **Goal**: Connect controllers, models, and views to build something meaningful. - βœ… **Create a Model**: ```csharp public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } } ``` - βœ… **Controller-Model Interaction**: ```csharp public IActionResult Details(int id) { var product = new Product { Id = id, Name = "Laptop", Price = 1200.00m }; return View(product); } ``` - βœ… **Pass Data to View**: - Use a strongly typed view (`@model Product`) - Display `@Model.Name`, `@Model.Price`, etc. - πŸ”² **Activity**: - Create `Product` model - Create `Details` and `List` actions - Create strongly-typed Razor views --- ### **7. Practice and Review: Small Project Assignment** **Goal**: Reinforce everything learned. - βœ… **Project: Product Management App** - CRUD: Create, Read, Update, Delete products - Use default and attribute routing - Return different types (View, JSON, Redirect) - Include HTTP verb handling - βœ… **Suggested Workflow**: 1. Create the `Product` model 2. Build controller actions 3. Set up routing 4. Implement Razor views 5. Test everything end-to-end - βœ… **Encourage Discussion**: - Why return JSON instead of HTML? - When to use attribute routing? - What's the benefit of redirecting? --- ## πŸ§ͺ Practice - Create a basic MVC Hello World app - Add `ProductController` with sample actions - Use `[Route]` and `[HttpGet]` in `CustomerController` - Implement full CRUD for customers or products - Add JSON, Content, and Redirect examples - Build a simple Product Detail View - Complete mini-project: Product Management App ## πŸ™ Acknowledgments Sources: - [w3schools.com](https://www.w3schools.com/) - [Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/) - ChatGPT conversations (2025 sessions)