ASP .NET Controller Base Class 🎯

beginner
25 min

ASP .NET Controller Base Class 🎯

Welcome to our comprehensive guide on the ASP .NET Controller Base Class! In this lesson, we'll explore this essential concept in depth, making it easy for both beginners and intermediates to understand. Let's dive right in!

What is a Controller? 📝

In ASP .NET, controllers handle incoming HTTP requests and generate responses. They act as an intermediary between your application and web API.

Understanding the Controller Base Class 💡

The System.Web.Mvc.Controller class is the base class for all controllers in ASP .NET MVC. It provides various methods and properties that simplify the process of handling HTTP requests and generating responses.

Key Methods in the Controller Base Class 💡

1. OnActionExecuting

This method is called before an action method is executed. It's useful for setting up shared resources or performing actions common to all action methods.

csharp
public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); // Your code here }

2. OnActionExecuted

This method is called after an action method is executed. It's useful for performing cleanup or logging.

csharp
public override void OnActionExecuted(ActionExecutedContext filterContext) { base.OnActionExecuted(filterContext); // Your code here }

3. OnResultExecuted

This method is called after the result is executed, such as sending a response to the client. It's useful for performing cleanup or logging after the result is sent.

csharp
public override void OnResultExecuted(ResultExecutedContext filterContext) { base.OnResultExecuted(filterContext); // Your code here }

Quiz 💡

Quick Quiz
Question 1 of 1

What does the `OnActionExecuted` method do?

Practical Example ✅

Let's create a simple controller with an action method and implement the OnActionExecuting method to demonstrate its usage.

csharp
using System.Web.Mvc; namespace YourNamespace { public class HomeController : Controller { public ActionResult Index() { return View(); } public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); // Perform action before the Index action method executes // For example, set a shared resource or perform a check if (User.IsInRole("Admin")) { filterContext.Controller.ViewData["Message"] = "Welcome, Admin!"; } } } }

In this example, we've created a HomeController with an action method Index. We've also implemented the OnActionExecuting method to check if the user is an admin and set a message in the view data if they are.

Remember, the base class Controller provides a solid foundation for creating controllers in ASP .NET MVC. Understanding the methods and properties it offers will help you build powerful and efficient web applications.

Happy coding! 🚀


By the way, if you found this tutorial helpful, don't forget to bookmark CodeYourCraft and share it with your friends! 🤝️ Let's build something amazing together! 🚀