为 ASP.NET Core 控制器自动生成 TypeScript 客户端代码自动生成、控制器、客户端、代码

2023-09-07 13:56:49 作者:你的名字再不是我的心事

我正在寻找一种从 ASP.NET Core Web 应用程序(当前使用 Visual Studio 2017RC 和 webpack)自动生成 TypesScript 客户端代码的方法.

I'm searching for a way to automatically generate the TypesScript client code from a ASP.NET Core Web-Application (Currently using Visual Studio 2017RC and webpack).

是否有任何现有的工具可以在 Visual Studio 的构建管道中或使用 webpack 生成 TypeScript 客户端?我尝试使用 swagger 中间件,然后从 swagger URL 生成到客户端,但我不确定中间 swagger 生成是否是适合这项工作的工具.此外,与构建工具的断开连接也不理想.

Are there any existing tools to generate the TypeScript client either in the build pipeline from visual studio or with webpack? I tried to use swagger middleware and then generate to client from the swagger URL, but I'm not sure if the intermediate swagger generation is the right tool for the job. Also the disconnectedness from the build tools is not ideal.

推荐答案

NSwag 和 Swashbuckle/swagger-codegen 很棒.

Typewriter 是另一个很棒的选择. 从零开始实现ASP.NET Core MVC的插件式开发 三 如何在运行时启用组件

Typewriter is another awesome option.

那么,我将如何做出决定:

So, here is how I would take the decision:

需要根据 C# 模型生成 TypeScript 模型,选择 TypeWriter

需要使用命令行生成完整的客户端代码,您可以将其添加为 Swagger Code Gen 的构建或发布步骤 Need to generate TypeScript Models as per the C# models, go for TypeWriter

Need to generate complete client side code with command line that you can add as a build or publish step go for Swagger Code Gen

需要使用生成的 Swagger 规范或生成的代码来添加您的自定义项 - 选择 NSwag

Need to play with the generated Swagger Spec or Generated Code to add your customizations - go for NSwag