Getting Started
Ce document est disponible en français
This page builds and runs the RDCore.Web host, writes a controller module, composes it into a program, and calls it. It assumes a command line,
Git, the .NET 11 SDK, and rdc, the RDCore command line, built from the
RDCore repository. It assumes no ASP.NET Core: the Overview says how RDCore.Web uses it.
1. Build and test
git clone https://github.com/rubberduck-vba/RDCore.Web.git
cd RDCore.Web
dotnet build RDCore.Web.slnx
dotnet test RDCore.Web.slnx
| Project | What it is |
|---|---|
RDCore.Web |
The framework library. |
RDCore.Web.Host |
The ASP.NET Core application that serves an application's endpoints and pages. |
RDCore.Web.Tests |
The tests. |
2. Run the host
dotnet run --project RDCore.Web.Host
The host writes the address it listens on, http://localhost:5000 by default; Kestrel
explains how to change it. Open that address in a browser: the host serves index.html from its wwwroot folder, a page with a Name box and a
Call button. Without a program, the host serves its page only. Press Ctrl+C to stop the host.
RDWebHostApplicationTests.TheRoot_ServesThePage_WhichLoadsItsScript pins the page.
3. Write a controller module
The page calls this endpoint, GET /api/hello/{name}. In a new folder, Hello, save this class module as Greetings.cls:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Greetings"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'@Controller("api")
Option Explicit
'@HttpGet("hello/{name}")
Public Function Hello(ByVal name As Variant) As Variant
Dim greeting As Object
Set greeting = CreateObject("Scripting.Dictionary")
greeting.Add "greeting", "Hello, " & name & "!"
Set Hello = greeting
End Function
| Line | What it declares |
|---|---|
'@Controller("api") |
The class module is a controller. Its route, api, prefixes the routes of its endpoints. It goes in the module's declarations section. |
'@HttpGet("hello/{name}") |
Hello serves GET requests on api/hello/{name}. It goes on the line above the procedure. |
{name} |
A route parameter: it binds the procedure's name parameter, as a string. |
A route parameter that names no parameter of the procedure is an error. The RDCore.Web Reference has the rules of each annotation.
RDWebEndpointTests.AnHttpGetAnnotation_ServesTheProcedureForGet_OnTheControllersRouteAndItsOwn and
RDWebEndpointsHostingTests.TheRouteValues_BindTheirParameters_AsStrings pin the endpoint and its binding.
4. Compose the program
From the Hello folder:
rdc new .
rdc compose .
rdc new writes the project file, .rdproj, with the folder's modules. rdc compose writes the program to bin/Debug/Hello.rdcomposition.
5. Run the host with the program
From the RDCore.Web folder, with the full path of the composition:
dotnet run --project RDCore.Web.Host -- --RDWeb:Composition C:\path\to\Hello\bin\Debug\Hello.rdcomposition
The path is a full one because dotnet run starts the host in its project folder, and a relative path is read from there. The host loads the program and
maps the endpoints of its controllers before it serves any request. §7.2 The RDCore.Web Host lists
its other options.
RDWebHostApplicationTests.AComposition_IsLoadedFromItsFullPath_WithTheHostAsItsMachinesExternalHost_AndItsControllersEndpointsAreServed pins the
loading and the endpoints.
6. Call it
Type a name on the page and select Call, or send the request from a command line:
curl.exe -i http://localhost:5000/api/hello/World
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{"greeting":"Hello, World!"}
The procedure's arguments and value pass as JSON: the Dictionary it returns is the response's JSON object. The request runs on a machine of the host's
pool, on a new instance of Greetings. RDWebEndpointsHostingTests.TheValueAProcedureReturns_IsTheJsonBodyOfTheResponse pins that response.
7. Describe it
The host serves an OpenAPI document of its endpoints:
curl.exe http://localhost:5000/openapi/v1.json
Its operation Greetings_Hello is GET /api/hello/{name}, with the JSON Schemas of the procedure's parameter and value. A client generator reads it:
§9.3 A TypeScript Client makes a typed fetch client from it.
RDWebHostApplicationTests.TheOpenApiDocument_IsServed_WithTheProgramsSchemas pins the document.
Open http://localhost:5000/swagger in a browser: the page lists the operation, and Try it out calls it with the name typed there. The host
serves the document and the page in the Development environment, which its launch settings give dotnet run; a host started otherwise serves them with
--RDWeb:OpenApi true.
Next
- Overview: how the parts fit together, and how they relate to ASP.NET Core.
- RDCore.Web Reference: each part's syntax and rules (English only).
- API reference: the .NET types (English only).
ACCUEIL • HOME | APERÇU • OVERVIEW | DÉMARRER • GET STARTED | RDCore | SDK | 🌐 rubberduckvba.ca