Controllers in ASP.NET
Learn about controller classes and their action methods in ASP.NET Core.
We'll cover the following...
Controller classes are where your application logic resides. At the end of this lesson, the entire controller is written in the code widget. Look at the code line by line to figure out how it is functioning.
Controller inheritance
public class UsersController : Controller
IController
interface. The Controller
class implements that interface while also providing you support for views. As a result, you inherit Controller
into your UserController
class.
Dependency injection
private readonly UsersContext _context;public UsersController(UsersContext context){_context = context;}
_context
throughout your controller class to access your database without having to initialize any object.
In your application, you have named your database context as UsersContext
. You can explore its code by going to Data
> UsersContext.cs
in the code widget at the end of this lesson.
Action methods
In UserController
and the name of an action method is Details
, then there is a URL endpoint ...