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
β Controllers and Actions
Understand how to create controllers and action methods, follow naming conventions, and return data to views.
π w3schools - MVC Controllers
β 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
β HTTP Methods in MVC
Learn how to respond to different types of HTTP requests using attributes like
[HttpGet]
,[HttpPost]
.
π HTTP Methods - MDN
β Return Types
Explore different return types like
ViewResult
,JsonResult
, and more, and know when to use each one.
π Action Results in ASP.NET Core
π 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
withIndex
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
).
- Be
-
β Basic Action Methods:
- Action methods must be
public
. - Default return type is usually
IActionResult
.
- Action methods must be
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.
- You can modify the pattern in
- β
Attribute-Based Routing:
- Useful for APIs or when you want specific paths.
[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 yourCustomerController
:[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.
- Use
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 toCustomerController
, 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.
- Use
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:
public class Product {
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
- β Controller-Model Interaction:
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.
- Use a strongly typed view (
- π² Activity:
- Create
Product
model - Create
Details
andList
actions - Create strongly-typed Razor views
- Create
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:
- Create the
Product
model - Build controller actions
- Set up routing
- Implement Razor views
- Test everything end-to-end
- Create the
- β
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]
inCustomerController
- 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
- Microsoft Learn
- ChatGPT conversations (2025 sessions)