This section explores the topic of koossery.MVCwin controllers, controller actions, and action results. After you complete this section, you will understand how controllers are used to control the way a user interacts with a koossery.MVCwin application.


Koossery.MVCwin_ControllerManager.png

Understanding Controllers

The ControllerManager is responsible for responding to requests made against a koossery.MVCwin application. It retrieves the target controller and executes the related action. Each request is mapped to a particular controller. For example, imagine that following code written in the OnClick’s button event.

this.Invoke(typeof(LoginController).Name, “Init”);

In this case, a controller named LoginController is invoked. The LoginController is responsible for generating the response to the ControllerManager object. For example, the controller might return a particular view back to the ControllerManager or the controller might redirect the user to another controller.

Listing 1 contains a simple controller named LoginController.

using System;
using System.Collections.Generic;
using System.Text;
using Koossery.MVCwin.Actions.itf;
using Koossery.MVCwin.Controller.impl;
using Koossery.MVCwin.Tuto.App_Code.controllers.controllerbase;
using Koossery.MVCwin.Tuto.App_Code.data.session;
using Koossery.MVCwin.Tuto.views.form;
using Koossery.MVCwin.Util;

namespace Koossery.MVCwin.Tuto.App_Code.controllers
{
	public class LoginController : ControllerBase
	{
		public IActionResult Init()
		{	//Clear all messages
			this.ClearErrorsAndMessage();

			//Display the loginView            
			return this.RenderView(typeof(LoginView).Name);
		}
	}
}

As you can see from Listing 1, a controller is just a class (a Visual Basic .NET or C# class). A controller is a class that derives from the base Koossery.MVCwin.controller.impl.ControllerBase class. By inherits from this base class, a controller inherits several useful methods (We discuss these methods in a moment).


Understanding Controller Actions

A controller exposes controller actions. An action is a method on a controller that gets called when a controller is invoked. For example, imagine that following code written in the OnClick’s button event.

this.Invoke(typeof(LoginController).Name, “Init”);

In this case, the Init() method is called on the LoginController class. The Init() method is an example of a controller action.

A controller action must be a public method of a controller class. C# methods, by default, are private methods. Realize that any public method that you add to a controller class is exposed as a controller action automatically.


Understanding Action Results

A controller action returns something called an action result. An action result is what a controller action returns in response to the ControllerManager object.

The koossery.MVCwin framework supports 2 types of action results including:
  1. ViewResult – Represents a Form
  2. RedirectToActionResult – Represents a redirection to a new action

All of these action results implements the IActionResult interface.

In most cases, a controller action returns a ViewResult. For example, the Init controller action in Listing 1 returns a ViewResult.

When an action returns a ViewResult, a Form is displayed. The Init() method in Listing 1 returns a view named LoginView to the ControllerManager.

Notice that the Init() action in Listing 1 does not return a ViewResult(). Instead, the RenderView() method of the Controller base class is called. Normally, you do not return an action result directly. Instead, you call one of the following methods of the Controller base class:
  1. View – Returns a ViewResult action result.
  2. RedirectToAction – Returns a RedirectToActionResult action result.

So, if you want to return a View to the ControllerManager, you call the RenderView(viewName) method. If you want to redirect the user from one controller action to another, you call the RedirectToAction() method. For example, the Details() action in Listing 2 redirects the user to the Init() action.

Listing 2 : HomeController

using System;
using System.ComponentModel;
using System.Text;
using KTMicroFI_WIN.data.util;
using KTMicroFI_WIN.views.forms;
using MVCWin.Actions.itf;
using Koossery.MVCwin.Controller.impl
namespace koosser.MVCwin.Tuto.controllers
{
	public class HomeController : ControllerBase
	{
		public IActionResult Init()
		{
			return this.RenderView(typeof(HomeView).Name);
		}
		public IActionResult Details()
		{
			return this.RedirectToAction(typefo(HomeController).Name, "Init");
		}
	}
}


Summary

The purpose of this section was to introduce you to the concepts of koossery.MVCwin controllers, controller actions, and controller action results. In the first section, you learned how to add new controllers to a koossery.MVCwin winform project. Next, you learned how public methods of a controller are exposed as controller actions. Finally, we discussed the different types of action results that can be returned from a controller action. In particular, we discussed how to return a ViewResult, RedirectToActionResult.

Last edited Jun 19, 2009 at 4:50 PM by mohamadou, version 6

Comments

No comments yet.