-
Notifications
You must be signed in to change notification settings - Fork 14
Tutorial 04 09 Add Controller Class
You're almost there, only two more steps to go, the next of which is to actually expose your Traditional Bridge routines as endpoints of your web service.
There are several ways that you might choose to do this, depending on what your routines do, and how they may or may not relate to existing functionality exposed by existing controllers in your service.
For example, if your service is exposing OData endpoints for Orders, and your Traditional Bridge routines also expose functionality related to Orders, you may choose to expose them via custom endpoint methods on your existing OrdersController class.
But it may be that your Traditional Bridge functions are not related to other controllers in your service. In that case, it probably makes sense to simply add a new custom controller class. That's the approach we'll take here.
-
In
Solution Explorer
, right-click on theServices.Controllers
class and selectAdd > Class
. -
Name the new class source file
TraditionalBridgeController.dbl
and click theAdd
button. -
Copy and paste the following code into the new class, replacing the default code:
;;***************************************************************************** ;; ;; Title: TraditionalBridgeController.dbl ;; ;; Description: WebAPI controller to expose example Traditional Bridge Routines ;; ;;***************************************************************************** import Microsoft.AspNetCore.Authorization import Microsoft.AspNetCore.Mvc import Microsoft.Extensions.Configuration import Microsoft.Extensions.Options import Newtonsoft.Json import System import System.Collections.Generic import System.Linq import System.Text import System.Threading.Tasks import Services.Controllers import Services.Models namespace Services.Controllers ;{Authorize} {Route("TraditionalBridge")} public partial class TraditionalBridgeController extends ControllerBase ;; Services provided via dependency injection private _TraditionalBridgeService, @TraditionalBridgeService public method TraditionalBridgeController aTraditionalBridgeService, @TraditionalBridgeService proc _TraditionalBridgeService = aTraditionalBridgeService endmethod endclass endnamespace
As you can see, the new class inherits from a base class named ControllerBase (actually Microsoft.AspNetCore.Mvc.ControllerBase), which in a nutshell means that it is an MVC Controller (previously referred to as a WebAPI controller).
Again, the dependency injection pattern is being used, this time to get hold of an instance of a class named TraditionalBridgeService
. Does that sound familiar? It should, it's the class that you wrote in the previous module of this tutorial, the one that exposes "wrapper" methods for your Traditional Bridge routines. Yes, it's all starting to come together!
Notice also that the class is decorated with an {Authorize}
attribute, but it is commented out. You should change this if authentication is enabled in your environment.
- If your environment uses authentication (or custom authentication) then remove the comment from the
{Authorize}
attribute.
Notice also that the controller class is also decorated with a {Route("TraditionalBridge")}
. This specifies that the endpoints in the controller will be available at the base address of the service, which on this occasion will NOT include the /odata/v1
part, because this controller is an MVC controller, not am OData controller.
- Save the code
Similar to when you built the TraditionalBridgeService
class, the next task is to add an endpoint method for each of the three Traditional Bridge routines being exposed:
-
Copy and paste the following code into the
TraditionalBridgeController
class, between the endmethod and endclass statements:{Route("GetEnvironment")} public async method GetEnvironment, @Task<IActionResult> proc mreturn ok(await _TraditionalBridgeService.GetEnvironment()) endmethod
As you can see, this endpoint is available at the URL TraditionalBridge/GetEnvironment
, and simply makes a call to the appropriate service wrapper method, returning to the client whatever string value comes back, embedded in a 200 OK
response. In this case that's really all that is needed, there really are no other valid response codes, and if there is a serious failure of any kind, MVC will trap the exception and return an HTTP 500 Internal Server Error to the client.
- Save the code
-
Copy and paste the following code into the
TraditionalBridgeController
class, between the endmethod and endclass statements:{Route("GetLogicalName/{aLogicalName}")} public async method GetLogicalName, @Task<IActionResult> required in aLogicalName, string proc mreturn ok(await _TraditionalBridgeService.GetLogicalName(aLogicalName)) endmethod
This endpoint method is very similar. The URL route is different of course, and there is now a parameter. This means that requests to the service must look something like this:
TraditionalBridge/GetLogicalName/DBLDIR
Where DBLDIR
is the name of the server-side logical name that we wish to retrieve the value for.
- Save the code
- Copy and paste the following code into the
TraditionalBridgeController
class, between the endmethod and endclass statements:
{Route("AddTwoNumbers/{aNumber1}/{aNumber2}")}
public async method GetAddTwoNumbers, @Task<IActionResult>
required in aNumber1, decimal
required in aNumber2, decimal
proc
mreturn ok(await _TraditionalBridgeService.AddTwoNumbers(aNumber1,aNumber2))
endmethod
And again, this third endpoint method is similar. Again a different URL route, and this time two query string based parameters, meaning that requests need to look something like this:
TraditionalBridge/AddTwoNumbers/1.1/2.2
Where 1.1
and '2.2' are the numbers that we wish to add together.
- Save the code
Before moving on, make sure the project builds:
-
Right-click on the
Services.Controllers
project and selectBuild
. -
Check the
Output
window and verify that the build was successful.1>------ Build started: Project: Services.Controllers, Configuration: Any CPU ------ ========== Build: 1 succeeded, 0 failed, 2 up-to-date, 0 skipped ==========
Next topic: Configure Traditional Bridge Environment
-
Tutorial 2: Building a Service from Scratch
- Creating a Basic Solution
- Enabling OData Support
- Configuring Self Hosting
- Entity Collection Endpoints
- API Documentation
- Single Entity Endpoints
- OData Query Support
- Alternate Key Endpoints
- Expanding Relations
- Postman Tests
- Supporting CRUD Operations
- Adding a Primary Key Factory
- Adding Create Endpoints
- Adding Upsert Endpoints
- Adding Patch Endpoints
- Adding Delete Endpoints
-
Harmony Core Code Generator
-
OData Aware Tools
-
Advanced Topics
- CLI Tool Customization
- Adapters
- API Versioning
- Authentication
- Authorization
- Collection Counts
- Customization File
- Custom Field Types
- Custom File Specs
- Custom Properties
- Customizing Generated Code
- Deploying to Linux
- Dynamic Call Protocol
- Environment Variables
- Field Security
- File I/O
- Improving AppSettings Processing
- Logging
- Optimistic Concurrency
- Multi-Tenancy
- Publishing in IIS
- Repeatable Unit Tests
- Stored Procedure Routing
- Suppressing OData Metadata
- Traditional Bridge
- Unit Testing
- EF Core Optimization
- Updating a Harmony Core Solution
- Updating to 3.1.90
- Creating a new Release
-
Background Information