Strongly Typed Client Web API Generators (WebApiClientGen) generate client API code in C# and TypeScript from ASP.NET Core Web API.
WebApiClientGen does not rely on Swagger/OpenAPI definitions. Instead, it walks through the runtime type information of a running Web API in a debug build, avoiding the inherent limitations of OpenAPI when analyzing .NET types and Web API behaviors, ultimately delivering a better developer experience for ASP.NET Core Web API developers, .NET client developer and TypeScript client developers.
The component set is released mostly through NuGet.
-
Fonlow.WebApiClientGenCore
for generating C# client API. And to generate TypeScript client APIs optimized for some development frameworks or libraries, add the following plugins:
-
Fonlow.TypeScriptCodeDOMCore
for developing TypeScript code generator through the CodeDOM approach.
-
Fonlow.Poco2TSCore
for developing TypeScript code generator through the CodeDOM approach for POCO and more.
-
Poco2TSCore.exe, a console app for generating TypeScript type interfaces from POCO.
-
Fonlow.DocCommentCore
for reading XML document of an .NET assembly
-
Fonlow.DataOnlyExtensions with JSON converters for handling date only scenarios between the clients and server which sit in different timezones. 3 NuGet packages
are available including one for .NET Framework.
You or your team is developing both Web API backend and client programs.
- Import Fonlow.WebApiClientGenCore, and optionally one of the plugins for popular JavaScript libs and frameworks.
- Add CodeGenController.cs to folder "Controllers"
- Ensure ApiExplorer visible through having the following in
program.cs:
builder.Services.AddControllers(configure =>
{
#if DEBUG
configure.Conventions.Add(new Fonlow.CodeDom.Web.ApiExplorerVisibilityEnabledConvention());//To make ApiExplorer be visible to WebApiClientGen
#endif
})
- Define the settings of the CodeGen.
- Launch the debug build of Web API
- Post the settings of the CodeGen to Web API.
Alternatively, you can use PowerShell scripts to automate. Please refer to these 2 examples:
Then you will get client codes like:
- .NET Client API in C#.
- Client API in TypeScript for Angular Reactive Forms or for Fetch.
- Client API codes generated are directly mapped from the runtime type information of the Web API controller methods, .NET primitive types and POCO classes.
- Doc comments of controller methods and POCO classes are copied.
- Some validation attributes are copied to C# client API codes.
- Some validation attributes are used to generate doc comments for TypeScript codes.
- Some validation attributes are transformed into validators of Angular Reactive Forms.
- .NET generic types are supported.
- WebApiClientGen is seamlessly integrated with ASP.NET Core Web API with very little steps/overheads to setup, maintain and synchronize between Web API and client APIs, during RAD or Agile Software Development.
- Support all .NET primitive types including decimal.
- Support DataTime, DataTimeOffset, DateOnly, Array, Tuple, Dynamic Object, Dictionary and KeyValuePair
- Strongly typed generated codes are subject to design time type checking and compile time type checking.
- Provide high level of abstraction, shielding application developers from repetitive technical details of RESTful practices and traditional codes of AJAX calls.
- Rich meta info including doc comments make IDE intellisense more helpful, so application developers have less need of reading separated API documents.
- Generated doc comments based on .NET validation attributes.
- Generated doc comments based on numeric types, DateOnly and GUID for TypeScript codes.
- Generated TypeScript codes conform to the TypeScript strict mode, and the generated Angular 2+ codes conform to the Angular strict mode.
- The format of generated TypeScript codes conform to the default of Visual Studio Code.
- POCO classes
- Web API
- Generated client API C# codes
- Client codes using the generated library in C#
- Generated client data models and API in TypeScript for Angular 2, for Fetch, for Aurelia and for Axios
- Client codes using the generated library in TypeScript
- Online Demo with Angular Heroes hosted in GitHub.IO talking to a real backend
Remarks:
- JavaScript codes compiled from generated TypeScript codes could be used in JS applications, however, obviously no type info will be available, while application programmers may still enjoy intellisense and abstraction from AJAX details.
- React and Vue.js applications typically use Axios or Fetch API for HTTP requests. Since June 2019, babel has supported namespaces thanks to this pull request, so you should be able to do React TSX programming with generated TypeScript codes.
- Web API vendors / developers should provide client API libraries to developers of client programs, as Google and Amazon etc. would do in order to make the RESTful or RPC Web API reach wider consumers (internal and external) efficiently.
- To client developers, classic function prototypes like
ReturnType DoSomething(Type1 t1, Type2 t2 ...)is the API function, and the rest is the technical implementation details of transportation: TCP/IP, HTTP, SOAP, resource-oriented, CRUD-based URIs, RESTful, XML and JSON etc. The function prototype and a piece of API document should be good enough for calling the API function. - The better you have separation of concerns in your Web API design, the more you will benefit from the components of this project in order to deliver business values sooner, with less handcrafted codes , less repetitive tasks and less chances of human mistakes.
ReturnType DoSomething(Type1 t1, Type2 t2 ...)
[HttpGet]
[Route("getPerson/{id}")]
public Person GetPerson(long id)Rather than writing explicit codes of validating the request payload, it is expected that you use middleware to validate. For example:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(
options =>
{
options.Filters.Add(new ValidateModelAttribute()); // wholesale style to check model binding for all API calls.References:
Model Validation through ApiControllerAttribute
For example:
[ApiController]
[Route("api/[controller]")]
public class HeroesController : ControllerBase
{Even if you explicitly write codes in an API function to handle exceptions and return non-2xx HTTP status code, you should have a safty net of catching uncaught exceptions and return HTTP status codes.
References:
- WIKI
- Settings Explained
- Generate C# .NET Client API for ASP.NET Web API / Local Copy
- Generate TypeScript Client API for ASP.NET Web API / Local Copy
- ASP.NET Web API, Angular2, TypeScript and WebApiClientGen / Local Copy
- Generate C# Client API for ASP.NET Core Web API / Local Copy
- Intended Solutions for Intentional Limitations of Strongly Typed OpenAPI Client Generators / Local Copy. The article is just using OpenApiClientGen as an example, while the principles and solutions can be applied to generated codes by WebApiClientGen for your client apps.
- DateOnly in ASP.NET Core 6 / Local Copy
The Demo applications in this repository are mainly for testing WebApiClientGen during development. And there are other demo applications in the following repositories, demostrating how real world applications could utilize WebApiClientGen:
- .NET Core Demo for ASP.NET Core MVC, Web API, ASP.NET Core + Angular, MAUI, fetchAPI, vue TS and React TS.
- WebApiClientGen Examples for .NET Framework, .NET Standard, Xamarin, and vue TS.
- WebApiClientGen vs Swagger
These demo applications are actively maintained and kept up-to-date with the latest frameworks. If you are still staying with some older frameworks like Angular 4 or 5 or .NET Core 2.0, you may navigate to respective tags of the repositories and checkout.
Tour of Heroes is the official Angular tutorial demo app.
To illustrate the programmer experience of using WebApiClientGen, the following demo apps are crafted with similar architectural design for the same functional features on various development frameworks or libraries, however, talking to a real backend.
- Angular 2+, and article: Generate Typed FormGroup of Angular Reactive Forms with ASP.NET Core Web API / Local Copy
- Xamarin
- MAUI. Migrated from Xamarin Heroes.
- Aurelia. Integration test suite included.
- React. Integration test suite included.
- Article: Tour of Heroes: Blazor WebAssembly Standalone App / Local Copy
While WebApiClientGen supports both, however, the primary support has shifted to System.Text.Json since 2024.
NewtonSoft.Json still has a few advantages upon certain scenarios and contexts:
- If you have a lot POCO classes decorated by DataContractAttributes, because of supporting legacy apps, or supporting both XML and JSON serialization, NewtonSoft.Json gives you inherent support, while System.Text.Json provides some troublesome and indirect support since .NET 7.
- For some array types and dynamic, NewtonSoft.Json is still better.
Hints:
- OpenApiClientGen based on key components of WebApiClientGen is a spin-off for generating client API codes in C# and TypeScript according to a definition file of Swagger/Open API Specification. Therefore the codes generated look fairly similar to what generated by WebApiClientGen.
Remarks:
- The development had started in year 2015 supporting .NET Framework, then .NET Core 2. And Tag "LastCore31" is to mark the last snapshot supporting .NET Framework 4.6.2 and .NET Core 3.1.
- Starting from 2021-02-10, the development will support only .NET 5 and onward.
- Wiki contents about .NET Framework will be kept in foreseeable future.
