In the first part of this article series about ASP.NET MVC
Framework, I gave a short introduction to Model View Controller (MVC) pattern,
ASP.NET MVC and the basic structure of the sample blogging engine that I am
going to build in this article series and have named it KBlog.
In this second part I am going to cover one of the main
three elements in MVC pattern which is controller.
Controller is the most common element of the MVC pattern and
is responsible for handling user inputs and executing appropriate codes to load
data and display them to the user.
Even though this is not a 100% correct definition, you can
consider controller as an intermediate component between the user and view
which tries to handle the user inputs and displays the appropriate output to
him or her.
Normally, you use some template files to handle different
requests in your web applications. It means that you have a page named
Post.aspx to handle requests to such a post and the same for other pages. In
MVC framework this is different and you use controller classes to handle
requests. In other words, you no longer handle user requests with .aspx pages. Instead,
you write controller classes that handle requests and pass appropriate data to
view pages to be shown to the end users.
But how do controller classes receive requests? To do this,
ASP.NET uses an URL routing mechanism that uses routes to route incoming requests
to appropriate controller classes. There is a default definition for routing in
ASP.NET MVC framework, but you can change it for your own needs as I will do
for KBlog in this article series. I will cover routing mechanism in the future
parts, but for this part I give a quick overview of the necessary parts.
The other topic that I will cover in this article is the
anatomy of controller classes and their structure as well as the action methods
and how to pass parameters to action methods in order to route requests to
them.