Page Controller pattern with an example in C#

|
| By Neeraj Sharma

The Page Controller pattern in C# is a design pattern used in web application development where each web page has its own controller to handle requests. This pattern is particularly useful in frameworks like ASP.NET MVC where the application is divided into a set of controllers, each responsible for a specific set of actions related to a particular page or a set of pages.

Key Features of Page Controller Pattern:

  1. Separation of Concerns: Each controller manages the processing logic for its associated page, keeping the logic for different pages separate.
  2. Modularity: Facilitates modular design, making it easier to maintain and update specific pages without affecting others.
  3. Routing: Each controller action is mapped to a specific route, making URL handling straightforward.

Example in C# using ASP.NET MVC

Suppose we have a simple web application with three pages: Home, About, and Contact. Here’s how we can implement the Page Controller pattern.

Step 1: Define the Controllers
HomeController.cs

using System.Web.Mvc;
public class HomeController : Controller
{
public ActionResult Index()
{
// Logic for handling the Home page request
ViewBag.Message = "Welcome to the Home Page!";
return View();
}
}

AboutController.cs

using System.Web.Mvc;
public class AboutController : Controller
{
public ActionResult Index()
{
// Logic for handling the About page request
ViewBag.Message = "Learn more About Us!";
return View();
}
}

ContactController.cs

using System.Web.Mvc;
public class ContactController : Controller
{
public ActionResult Index()
{
// Logic for handling the Contact page request
ViewBag.Message = "Get in touch with us!";
return View();
}
}

Step 2: Define the Views

Create corresponding views for each controller action.
Views/Home/Index.cshtml

@{
ViewBag.Title = "Home";
}

@ViewBag.Message

 

Views/About/Index.cshtml

@{
ViewBag.Title = "About";
}

@ViewBag.Message

 

Views/Contact/Index.cshtml

@{
ViewBag.Title = "Contact";
}

@ViewBag.Message

 

Step 3: Configure the Routes

Ensure that the routes in your RouteConfig.cs file (located in the App_Start folder) are direct to the correct controllers.

RouteConfig.cs

using System.Web.Mvc;
using System.Web.Routing;

public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

How It Works

  • HomeController: Handles requests to the home page (e.g., /Home/Index).
  • AboutController: Manages the about page requests (e.g., /About/Index).
  • ContactController: Deals with contact page requests (e.g., /Contact/Index).

Each controller is responsible for a single page or a related set of pages, encapsulating all the logic required to render those pages. This makes the code easier to manage and maintain as the application grows.

Benefits

  • Maintainability: Changes to the logic of a specific page are confined to its controller, making the application easier to manage.
  • Scalability: New pages can be added by creating new controllers, without affecting existing functionality.
  • Clarity: By keeping the logic for each page in its controller, the application structure is clear and logical.

By using the Page Controller pattern in an ASP.NET MVC application, you ensure that each page’s request handling is cleanly separated and easy to manage, contributing to a well-organized and scalable web application architecture.

Leave a Reply

Your email address will not be published. Required fields are marked *