Controllers and Actions in MVC
Every request that comes to our application is handled by a controller. The controller is free to handle the request any way it wants, as long as it doesn’t belong into the areas of responsibility of the model and view. This means that we do not put business or data storage logic into a controller, nor can we generate user interfaces.
In the ASP.NET MVC Framework, controllers are .NET classes that contain the logic required to handle a request. The role of the controller is to encapsulate your application logic. This means that controllers are responsible for processing incoming requests, performing operations on the domain model, and selecting views to render to the user.
In the MVC Framework, controller classes must implement the IController interface from the System.Web.Mvc namespace.
The MVC Framework is very flexible. You can implement the IController interface to create any kind of request handling and result generation you require.
The Controller class provides three key features:
• Action methods: A controller’s behavior is partitioned into multiple methods (instead of having just one single Execute() method). Each action method is exposed on a different URL and is invoked with parameters extracted from the incoming request.
• Action results: You can return an object describing the result of an action (for example, rendering a view, or redirecting to a different URL or action method), which is then carried out on your behalf. The separation between specifying results and executing them simplifies unit testing.
• Filters: You can encapsulate reusable behaviors as filters, and then tag each behavior onto one or more controllers or action methods by putting an [Attribute] in your source code.
A Controller exposes Controller Actions. An action is a method on a controller that gets called when you enter a particular URL in the browser address bar. For example let us imagine we make a request for the following URL-
In this case the Index method is called on the Employee Controller class. The Index method is an example of a controller action.
A controller action must be a public method of a controller class. By default C# methods are private. We should note that any public method we add to the controller class is exposed as a controller action automatically.
Controller actions return something known as Action Results.
ASP.NET Framework supports several types of action results-
- ViewResult - Represents HTML and markup.
- EmptyResult - Represents no result.
- RedirectResult - Represents a redirection to a new URL.
- ContentResult - Represents a text result.
- FileContentResult - Represents a downloadable file (with the binary content).
- FilePathResult - Represents a downloadable file (with a path).
- FileStreamResult - Represents a downloadable file (with a file stream).
Let us see this through an example-
public class EmployeeController : Controller
public ActionResult Index()
// Add action logic here
In the above example the Index action returns a view result. Note that to return a view result the View() method is called. In the same way we call different methods to return any of the action results. We cannot return Action results directly.
Few built in Action results along with their methods is given below-
ViewResult - View()
PartialViewResult - PartialView()
RedirectResult - Redirect(),RedirectPermanent()
HttpNotFoundResult - HttpNotFound()