Curriculum
Controllers and Actions in ASP.NET Core MVC are the heart of every MVC application. A Controller receives user requests, processes application logic, communicates with models and services, and returns responses to users. Actions are methods inside controllers that handle specific requests and generate results.
Understanding Controllers and Actions in ASP.NET Core MVC is essential because every ASP.NET Core MVC application relies on controllers to process requests and actions to execute application functionality. Whether you are building an E-Commerce Platform, School Management System, Hospital Management System, Banking Application, CRM, ERP, or Enterprise Portal, controllers play a critical role in application architecture.
A Controller is a class responsible for handling incoming HTTP requests.
Responsibilities:
Receive Requests
Process Logic
Interact with Models
Call Services
Return Responses
Controllers act as the bridge between users and the application.
Without controllers:
No Request Handling
No Business Flow
No User Interaction
Controllers coordinate application behavior.
User Request
↓
Controller
↓
Model
↓
Database
↓
Controller
↓
View
↓
Response
The controller sits at the center of the MVC architecture.
Controllers are typically stored inside the:
Controllers Folder
Example:
using Microsoft.AspNetCore.Mvc;
public class
StudentController :
Controller
{
}
This creates a controller named StudentController.
Controllers should end with:
Controller
Examples:
HomeController
StudentController
CourseController
AccountController
AdminController
Following naming conventions helps ASP.NET Core identify controllers automatically.
Example:
public class
StudentController :
Controller
{
}
The Controller base class provides:
View()
Redirect()
Json()
Content()
ModelState
These features simplify development.
Actions are public methods inside a controller.
Example:
public IActionResult
Index()
{
return View();
}
The method:
Index()
is called an Action Method.
Actions:
Handle Requests
Execute Logic
Return Results
Every request is eventually processed by an action.
Example:
public IActionResult
Index()
{
return Content(
"Welcome Students");
}
Output:
Welcome Students
The action returns text directly.
Controller:
StudentController
Action:
Index
URL:
/Student/Index
Output:
Welcome Students
ASP.NET Core maps the URL to the action automatically.
Most actions return:
IActionResult
Why?
Because an action can return different types of responses.
Examples:
View
JSON
File
Redirect
Content
IActionResult provides flexibility.
Example:
public IActionResult
Index()
{
return View();
}
ASP.NET Core searches for:
Views
↓
Student
↓
Index.cshtml
The view is rendered automatically.
Example:
public IActionResult
About()
{
return Content(
"About Us Page");
}
Output:
About Us Page
Useful for testing and simple responses.
Example:
public IActionResult
GetStudent()
{
return Json(
new
{
Id = 1,
Name = "Rahul"
});
}
Output:
{
"id": 1,
"name": "Rahul"
}
Common in API development.
Example:
public IActionResult
GoHome()
{
return Redirect(
"/Home");
}
Output:
Redirect User
Useful after form submissions.
Example:
public class
StudentController :
Controller
{
public IActionResult
Index()
{
return View();
}
public IActionResult
Details()
{
return View();
}
public IActionResult
Contact()
{
return View();
}
}
One controller can contain many actions.
Use meaningful names:
Index
Details
Create
Edit
Delete
Contact
Avoid unclear names.
By convention:
Index
is considered the default action.
Example:
/Student
automatically executes:
Index()
This simplifies URLs.
Actions can accept parameters.
Example:
public IActionResult
Details(
int id)
{
return Content(
$"Student {id}");
}
URL:
/Student/Details/5
Output:
Student 5
Parameters allow dynamic processing.
Example:
public IActionResult
Course(
int id,
string name)
{
return Content(
$"{id} {name}");
}
URL:
/Student/Course?id=1&name=.NET
Output:
1 .NET
Query string values are automatically mapped.
Example:
[HttpGet]
public IActionResult
Create()
{
return View();
}
Purpose:
Display Form
GET requests retrieve information.
Example:
[HttpPost]
public IActionResult
Create(
Student student)
{
return RedirectToAction(
"Index");
}
Purpose:
Submit Form Data
POST requests create or update data.
Filters execute before or after actions.
Examples:
Authorization
Logging
Exception Handling
Filters help manage cross-cutting concerns.
Request:
/Student/Details/1
Processing:
StudentController
↓
Details()
↓
Database Query
↓
View Returned
The student details page appears.
Controllers:
AccountController
TransactionController
Controllers:
ProductController
OrderController
CartController
Controllers:
PatientController
DoctorController
Controllers:
StudentController
CourseController
AttendanceController
Controllers organize application functionality.
Code remains structured.
Responsibilities are separated.
Actions can be reused.
Applications grow more easily.
Controllers can be unit tested.
These advantages make MVC powerful.
Controllers should remain focused.
Move logic to services.
Use meaningful naming conventions.
Extract reusable functionality.
Always validate user input.
A Controller handles incoming requests and returns responses.
An Action Method is a public method inside a controller that processes requests.
IActionResult is a flexible return type for MVC actions.
Index is the default action by convention.
GET retrieves data, while POST submits data.
They process requests, coordinate application logic, and return responses.
A Controller is a class responsible for handling requests and returning responses.
An Action Method is a public method inside a controller that processes requests.
IActionResult is a common return type that allows actions to return different response formats.
Index is the default action used by ASP.NET Core MVC.
Yes, action methods can accept route values, query string parameters, and form data.
They form the core request-processing mechanism of MVC applications.
WhatsApp us