Provide API metadata

In order to call REST API from DotVVM, the API needs to provide Open API metadata.

ASP.NET Core or ASP.NET Web API

If you decide to build the REST API using ASP.NET Core or ASP.NET Web API, there are NuGet packages you can use to allow sharing data models between DotVVM application and the REST API.

These NuGet packages work with Swashbuckle, a popular library that generates Open API metadata.

First, make sure you have Swashbuckle installed and configured in your project.

Then install the following NuGet package to the REST API project:

Install-Package DotVVM.Api.Swashbuckle.AspNetCore

# optional - install only if you are using Newtonsoft.Json
Install-Package Swashbuckle.AspNetCore.Newtonsoft

To enable DotVVM integration, call the EnableDotvvmIntegration extension method in the Swashbuckle configuration in Startup.cs file.

services.Configure<DotvvmApiOptions>(opt => 
{
    // TODO: configure DotVVM Swashbuckle options
});

services.AddSwaggerGen(options => {
    ...
    options.EnableDotvvmIntegration();
});

Optional: In case of DotVVM 2.5 or newer with ASP.NET Core 2.x, or ASP.NET Core 3.x with Newtonsoft.Json serializer, you also need to call AddSwaggerGenNewtonsoftSupport.

services.AddSwaggerGenNewtonsoftSupport();

Share models between API and DotVVM app

By default, DotVVM CLI generates classes for all types used in the REST API (in both C# and TypeScript clients). For example, if the API returns a list of orders, there will be the Order class in the generated client.

If the API is hosted in the same project as the DotVVM application, or if the API project can share these types with the DotVVM application using a class library, you can register these types as known types. In such case, they won't be included in the generated clients, and you just need to make sure both DotVVM and API can see these types with the same name and namespace.

To register known types, configure the DotVVM integration like this:

services.Configure<DotvvmApiOptions>(opt => 
{
    // add a single type
    opt.AddKnownType(typeof(Order));

    // add all types from the assembly
    opt.AddKnownAssembly(typeof(Order).Assembly);

    // add all types from the namespace
    opt.AddKnownNamespace(typeof(Order).Namespace);
});

Azure Functions

If you want to consume Azure Functions, you can refer to the Open API definition tutorial in the official docs.

Other technologies

If the API is built with different technology, you'll need to obtain the Open API specification of the API. Most frameworks should be able to generate the document for you.

Alternatively, you can build it yourself in Swagger Editor.

See also