Sau bài này, bạn sẽ biết:
Controller Rendering là gì
Cách tạo View + Controller + Model trong Sitecore MVC
Cách đăng ký Rendering trong Sitecore
Cách gắn Rendering lên page
Cách debug & best practices
Trong Sitecore có 3 loại rendering chính:
| Rendering | Mục đích |
|---|---|
| View Rendering | Hiển thị HTML đơn giản, không cần controller |
| Controller Rendering | Xử lý logic backend → trả về model → render view |
| JSON Rendering | Dùng cho Headless |
Controller Rendering = “ASP.NET MVC bình thường” nhưng chạy trong Sitecore.
Giả sử bạn muốn tạo component Navigation Menu.
/src/Feature/Navigation/Models/MenuViewModel.cs
/src/Feature/Navigation/Controllers/MenuController.cs
Lưu ý quan trọng về Sitecore
Phải trả về View bằng path tuyệt đối.
/Views/Feature/Navigation/MainMenu.cshtml
Vào Sitecore Content Editor:
Ở panel bên phải:
| Field | Giá trị |
|---|---|
| Controller | Menu |
| Controller Action | MainMenu |
| Datasource Template (optional) | Không cần hoặc chọn Template phù hợp |
| Datasource Location | /sitecore/content/Data/... |
Nếu bạn dùng Helix → đặt đúng folder Feature/Navigation.
Mở Experience Editor
Chọn placeholder (ví dụ: navigation)
Add Rendering → chọn Main Menu
Publish → xem kết quả
Khi load trang, Sitecore sẽ:
Gọi MenuController.MainMenu()
Tạo MenuViewModel
Render view tại /Views/Feature/Navigation/MainMenu.cshtml
Xuất HTML ra trang
Nếu bạn thấy menu → 🎉 Bạn đã tạo được Controller Rendering đầu tiên.
absolute path khi return viewVí dụ xử lý datasource null: