Tutorials ASP.NET Core MVC Tutorial
Creating Controllers — Complete Guide
Creating Controllers — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ASP.NET Core MVC Tutorial on Toolliyo Academy.
On this page
ASP.NET Core MVC Tutorial · Lesson 32 of 200
Creating Controllers
Getting Started ✓ → Core MVC → Data & Security → Production → Career
Beginner · 3 — Controllers & Views · ~6 min · Section 3: Controllers
What is this?
A controller is a C# class that handles web requests. Each public method is an action — like Index for a list page or Details for one item.
Why should you care?
URLs map to controller actions. Without controllers, your server would not know what code to run for /Cart or /Checkout.
See it live — copy this example
Create an MVC project (dotnet new mvc), add the code, and run dotnet run.
public class ProductsController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Details(int id)
{
ViewData["ProductId"] = id;
return View();
}
}
Run Example »
Edit the code and click Run — like W3Schools Try it Yourself.
What happened?
- Controller base class gives you helpers like View() and RedirectToAction().
- The name ProductsController maps to /Products/...
- URLs by convention.
Try it yourself
- Add ProductsController with Index and Details actions.
- Create matching views under Views/Products/.
- Browse to /Products/Details/42 and show id in the view.
- Change text or labels in the example and run again — watch the browser update.
- Break the code on purpose (remove a semicolon), read the error message, then fix it.
Remember
Controllers receive HTTP requests. Actions return IActionResult (usually View or Redirect). Class name minus "Controller" becomes the URL segment.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!