Building a Simple Blog Engine with ASP.NET MVC and LINQ - Part 2
 
Published: 30 Jan 2008
Abstract
In the second part of the article series about ASP.NET MVC Framework, Keyvan adds controllers to his blogging engine in order to describe how to use controllers in ASP.NET MVC and discusses some details related to controllers. He first discusses the concept of URL routing patterns and then explores the anatomy of a controller class. Finally, he examines how to implement the controllers in his sample blog application.
by Keyvan Nayyeri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 55885/ 91

Introduction

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.

Default URL Routing Pattern

As I stated, there is a default routing pattern for routing mechanism in ASP.NET MVC framework that you can modify for your own. This mechanism can be modified in the ASP.NET Application Class (Global.asax file).

But here I do not care about routing mechanism details and how to modify it. A future article of this series covers this in good detail, but I just talk quickly about the default routing pattern.

By default, ASP.NET MVC framework routes requests to /Url/ paths to a controller class with UrlController name. As an instance, requests to /Post/ URL will be routed to PostController class by default.

Moreover, all requests to URL's under /Url/ path will be routed to UrlController class as well. For instance, requests to /Post/Technology/Surface will be routed to PostController class.

The Anatomy of a Controller Class

A controller class is a class just like other classes. It is not necessary to derive this class from a special class or implement an interface for it, but it is recommended to derive a controller class from System.Web.MVC.Controller base class for ease of development and getting the benefits of this base class in your development.

Controller class provides some tools that can assist you on developing your ASP.NET MVC applications and I do not think any developer wants to avoid using it. Controller class is applied to all project and file item templates in Visual Studio and whenever you create a new MVC controller class in Visual Studio this class appears in your code.

You can add a new MVC controller class file in the Add New Item dialog as is shown in Figure 1.

Figure 1

Listing 1 shows the default code for a controller class that I named SampleController.

Listing 1

using System;
using System.Web;
using System.Web.Mvc;
 
namespace KBlog.Controllers
{
    public class SampleController : Controller
    {
        [ControllerAction]
        public void Index()
        {
            //Add action logic here
        }
    }
}

As you see, there is a reference to System.Web.Mvc namespace which comes to your machine after installing the ASP.NET 3.5 extensions and the class is also derived from the Controller base class.

Action Methods

The main part of a controller class is its action methods. Action methods are just some public methods that do not return anything and are marked with a ControllerAction attribute which makes a method an action method.

But what is the responsibility of an action method? Well, an action method receives a request and triggers appropriate logic to fetch data from the data model and passes this data to views in order to show the appropriate result to end users. So action methods are like the heart of the controller class.

But how do action methods work? Action methods work based on the parameters that are passed to them. Obviously, URL's represent the content that a user is looking for and routing mechanism routes each request to the appropriate controller class. But here, parameters specify the action that should take place.

For instance, when the user is viewing a page like /Post?ID=5 then the request should be routed to PostController class where Post action method handles the request in some ways.

Listing 2 adds an action method to Listing 1.

Listing 2

using System;
using System.Web;
using System.Web.Mvc;
 
namespace KBlog.Controllers
{
    public class SampleController : Controller
    {
        [ControllerAction]
        public void Index()
        {
            //Add action logic here
        }
    }
}

By default, there is a HomeController class in ASP.NET MVC projects that handles requests to default home pages.

In the next section I will discuss more about the parameters in action methods.

Parameters in Action Methods

Parameters are the key part of the controller class and specify the user's choice to actions methods. Parameters come directly from the URL and its query parameters and you need to decide how to use them with your own logic.

There are some different ways to handle parameters in action methods and it is left to you to decide how to do this.

The first approach is to use query parameters and fetch them in the action method from the URL. Obviously, this approach does not need any parameter for the action method. Listing 3 shows such an action method that retrieves the ID parameter from query string.

Listing 3

// Sample URL: /Page/Article?ID=5
[ControllerAction]
public void Article()
{
    int id = Convert.ToInt32(Request["ID"]);
}

The first approach was something that you could do in all ASP.NET web applications as well. In the second approach you pass the URL parameter to the action method as its parameter by defining the type.

Listing 4 shows an example of this approach. You can simply use the id parameter in your action method to write your logic.

Listing 4:

// Sample URL: /Page/Article?ID=5
[ControllerAction]
public void Article(int id)
{
    // Implement the logic to handle the
    // request based on the id parameter
}

The last way is similar to the previous one. You handle parameters via action method parameters, but parameters also come from sub-paths. So if you have a URL like /Page/Article/5, then 5 can be considered as the ID parameter to be passed to the controller.

Listing 5 shows this in action.

Listing 5:

// Sample URL: /Page/Article/5
[ControllerAction]
public void Article(int id)
{
    // Implement the logic to handle the
    // request based on the id parameter
}

You can use nullable type arguments in order to pass optional parameters to an action method. For instance, suppose that you have an article that consists of several sections. In one case the user can request the whole article and in an optional case he can also pass a Section parameter to get a specific section based on its unique string title.

In listing 6 you see how this can be implemented.

Listing 6:

// Sample URL 1: /Page/Article/5
// Sample URL 2: /Page/Article/5?section=introduction
[ControllerAction]
public void Article(int id, string? section)
{
    // Implement the logic to handle the request
    // based on the id and/or section parameters
}
Implementing the Controllers for KBlog

Now that we have gotten familiar with the principles of controller classes, action methods and action parameters, we can apply them in KBlog to update our code.

For KBlog we need three controllers (regarding that here we just want to build a very simple blogging engine to show some concepts):

·         Controller for homepage which is our index: We load data for recent N posts and the list of all categories.

·         Controller for individual posts: We load data for a specific post based on its ID.

·         Controller for individual categories: We load all posts that are in a specified category based on its unique name.

Listing 7 is the code for the homepage in HomeController where we do not need to pass any parameter. Later I will update this controller to load recent posts and all categories and pass them to the appropriate view. For now, just focus on the controller part of the code.

Listing 7:

using System;
using System.Web;
using System.Web.Mvc;
 
namespace KBlog.Controllers
{
    public class HomeController : Controller
    {
        // Sample URL: /Default.aspx
        [ControllerAction]
        public void Index()
        {
 
        }
    }
}

The other controller is PostsController where we handle requests for individual posts based on the unique ID (Listing 8). Note that later we need to modify the default routing definitions in order to route request to this controller.

Listing 8:

using System;
using System.Web;
using System.Web.Mvc;
 
namespace KBlog.Controllers
{
    public class PostsController : Controller
    {
        // Sample URL: /Post/25
        [ControllerAction]
        public void Post(int id)
        {
 
        }
    }
}

And the last controller is CategoriesController where we handle requests for a specific category based on its unique string name and load all posts in that category (Listing 9). Like PostsController we need to modify default routing definition in order to route requests to this controller.

Listing 9:

using System;
using System.Web;
using System.Web.Mvc;
 
namespace KBlog.Controllers
{
    public class CategoriesController : Controller
    {
        // Sample URL: /Category/DotNet
        [ControllerAction]
        public void Category(string name)
        {
 
        }
    }
}
Summary

In the second part of my articles about ASP.NET 3.5 MVC framework I discussed controller as one of the main parts of the MVC pattern.

First, I introduced controllers then talked about the anatomy of controller classes in ASP.NET MVC. Then I talked about action methods and their parameters. Finally, I applied these theories in KBlog to move forward in the development of the simple sample blogging engine that I am going to write in this article series.

In the future parts I will talk about data model, views, unit testing of MVC applications, URL routing and other related ideas.



User Comments

Title: ASP .Net Developer   
Name: ASP .Net Developer
Date: 2010-05-21 7:26:52 AM
Comment:
Hi
Building a Blog engine throw asp .net is cool.
thanks for this.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-06-24 1:49:15 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search