Programming in the Controller for MVC Web

The controller is implemented as a C# class. The code in the controller executes on the web server system. For an application in production, the web server system is the ePortal platform. When you test your presentation project during development, the web server system is your development system. The purpose of the controller is to respond to HTTP requests and determine the action to take based upon the content of the incoming request. For more information on controllers, refer to http://www.asp.net/mvc/overview/controllers-and-routing.

When you scaffold an ePortal MVC Web presentation project using the Scaffold Unisys Controller for ePortal Data Source with Views Dialog or Scaffold Unisys Controller for ePortal Data Source Dialog, ePortal generates a controller for you in the file Controllers/<controllername>Controller.cs; however, you may wish to override methods in the generated controller, or add new methods to the controller.

Refer to Creating an MVC Web Application for more information.

Notes:

To program in the controller, you need to create a new class file that implements a public partial class. A partial class allows you to define the code for a single class in multiple physical files. Perform the following steps:

  1. In Solution Explorer, navigate to the Controllers folder for your MVC project.

  2. Right-click the <controllername>Controller.cs file and select Copy, then right-click the Controllers folder and select Paste.

  3. Rename the new file to something appropriate, such as <controllername>ControllerOverrides.cs.

  4. Double-click the new file to open it in the editor.

  5. Delete the entire class <controllername>ControllerBase. You can do this easily by clicking the minus sign in the margin to collapse the base class to a single line, then select the class and delete.

  6. Inside the braces for the <controllername>Controller class, you can implement new methods or override existing methods.

    For example, to override an existing method, go to a new line and type “public override” and press the space bar. Intellisense provides a list of all of the methods that you can override. Double-click your choice from the list to override that method.

Your code can also write messages to the ePortal log for debug or auditing purposes; refer to Logging Messages from your Code and Logging Errors and Tracing for more information.

The following is a simple example of an override file for a controller named “form”. It shows overriding a method named “Login” and writing a message to the ePortal log.

using System.Web.Mvc;
using Unisys.Common.Shared;
    namespace eBankMvc.Controllers
{
     public partial class formController : formControllerBase   
    {
        public override ActionResult Login(eBankDataSource.Client.Login model)
        {
            Logging.WriteLogs(Logging.LogLevel.Information, "About to call Login for user {0}",              
                  model.Usercode);
            return base.Login(model);
        }   
    }
}