为 ASP.NET Core 控制器自动生成 TypeScript 客户端代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41275577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically generate TypeScript client code for ASP.NET Core controllers
提问by Fionn
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).
我正在寻找一种从 ASP.NET Core Web 应用程序(当前使用 Visual Studio 2017RC 和 webpack)自动生成 TypesScript 客户端代码的方法。
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.
是否有任何现有工具可以在 Visual Studio 的构建管道中或使用 webpack 生成 TypeScript 客户端?我尝试使用 swagger 中间件,然后从 swagger URL 生成到客户端,但我不确定中间 swagger 生成是否适合这项工作。此外,与构建工具的断开连接也不理想。
回答by Gaurav Madaan
Both NSwagand Swashbuckle/swagger-codegenare awesome.
Typewriteris another awesome option.
So, here is how I would take the decision:
- 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
Need to play with the generated Swagger Spec or Generated Code to add your customizations - go for NSwag
- Need to show Client inline code snippets in Swagger UI, use SwaggerUI-snippets
无论NSwag和Swashbuckle /招摇,代码生成是真棒。
打字机是另一个很棒的选择。
所以,这是我将如何做出决定:
- 需要根据 C# 模型生成 TypeScript 模型,请选择 TypeWriter
需要使用命令行生成完整的客户端代码,您可以将其添加为 Swagger Code Gen 的构建或发布步骤
需要使用生成的 Swagger 规范或生成的代码来添加您的自定义 - 去 NSwag
- 需要在 Swagger UI 中显示客户端内联代码片段,使用SwaggerUI-snippets
回答by William Cheng
You can add Swashbuckleto your ASP.NET Core webapp and then generate Swagger/OpenAPI spec.
您可以将Swashbuckle添加到您的 ASP.NET Core Web 应用程序,然后生成Swagger/OpenAPI 规范。
With the Swagger/OpenAPI spec, you can then use Swagger Codegen to generate not only TypeScript API clients (Node, Angular, Angular2, Fetch), but also API clients in C#, Java, JS, Swift, etc as well as API documentation.
借助 Swagger/OpenAPI 规范,您不仅可以使用 Swagger Codegen 生成 TypeScript API 客户端(Node、Angular、Angular2、Fetch),还可以生成 C#、Java、JS、Swift 等的 API 客户端以及 API 文档。
The easiest way to try Swagger Codegen is to use https://editor.swagger.ioby importing your spec and then select the target API client/server you want to generate. (https://editor.swagger.ioleverages https://generator.swagger.ioto generate code and generator.swagger.io is powered by Swagger Codegen)
尝试 Swagger Codegen 的最简单方法是通过导入您的规范来使用https://editor.swagger.io,然后选择您想要生成的目标 API 客户端/服务器。(https://editor.swagger.io利用https://generator.swagger.io生成代码,generator.swagger.io 由 Swagger Codegen 提供支持)
Swagger Codegen can also generate server stubs in C# Nancyfx or ASP.NET core so you may consider following the contract-first approach by writing the spec using https://editor.swagger.ioand then generating both API clients and server stubs.
Swagger Codegen 还可以在 C# Nancyfx 或 ASP.NET 核心中生成服务器存根,因此您可以考虑遵循契约优先方法,使用https://editor.swagger.io编写规范,然后生成 API 客户端和服务器存根。
UPDATE: On May 2018, about 50 top contributors and template creators of Swagger Codegen decided to fork Swagger Codegen to maintain a community-driven version called OpenAPI Generator. Please refer to the Q&Afor more information.
更新:2018 年 5 月,大约 50 名 Swagger Codegen 的顶级贡献者和模板创建者决定分叉 Swagger Codegen 以维护一个名为OpenAPI Generator的社区驱动版本。请参阅问答以获取更多信息。
回答by Rico Suter
Another option is NSwag which can generate Swagger specs from your ASP.NET (Core) controllers and TypeScript clients from this Swagger spec(for Angular, AngularJS, Fetch, etc.). The integration is very simple and it supports many advanced features (inheritance, named enums, code extensions, etc).
另一种选择是NSwag,它可以从你的 ASP.NET (Core) 控制器和 TypeScript 客户端从这个 Swagger 规范(用于 Angular、AngularJS、Fetch 等)生成 Swagger 规范。集成非常简单,它支持许多高级功能(继承、命名枚举、代码扩展等)。
Using the NSwagStudio UI it is very simple to get started and then you can automate everything with NPM package (i.e. running the config file in the cmd line).
使用 NSwagStudio UI 开始非常简单,然后您可以使用 NPM 包自动执行所有操作(即在 cmd 行中运行配置文件)。
I'm one of the authors of the project.
我是该项目的作者之一。
回答by Amar Zavery - MSFT
You can also take a look at autorest.typescriptclient code generator. It provides good interfaces for all the model definitions, accurately defines readonly properties and enums. It supports a bunch of custom extensionsthat can be useful to improve the user experience of the generated client. You can take a look at some generated sample clients.
您还可以查看autorest.typescript客户端代码生成器。它为所有模型定义提供了良好的接口,准确定义了只读属性和枚举。它支持一系列自定义扩展,可用于改善生成的客户端的用户体验。您可以查看一些生成的示例客户端。
Bonus: The generated typescript client works in node.js and also in the browser.
额外奖励:生成的 typescript 客户端可以在 node.js 和浏览器中使用。
回答by ZZZ
If your TypeScript clients are with jQuery or Angular 2+, an alternative ASP.NET Web API Client Generatorsmay be more handy, less overhead than swagger toolchain during SDLC.
如果您的 TypeScript 客户端使用 jQuery 或 Angular 2+,则在 SDLC 期间,替代的ASP.NET Web API 客户端生成器可能比 swagger 工具链更方便,开销更少。
And it supports .NET Core in addition to .NET Framework and .NET Standard.
除了 .NET Framework 和 .NET Standard,它还支持 .NET Core。
回答by Alex Klaus
If you need to generate DTOs and API clients, then your options are
如果您需要生成 DTO 和 API 客户端,那么您的选择是
- NSwag. The most popular tool for the job. But it generates everything in one file that makes it hard to maintain/debug.
- Swagger Codegenor its fork with more features OpenAPI Generator. A Java-based option producing separate files, but with limited customisation.
- AutoRest. A npm solution for packing up API clients in npm packages.
- WebApiClientGen. A solution mentioned by the author belowin this thread.
- TypeWriter. VS extension, where you can craft your own implementation.
- 斯瓦格。最流行的工作工具。但它在一个文件中生成所有内容,这使得维护/调试变得困难。
- Swagger Codegen或其分支具有更多功能OpenAPI Generator。基于 Java 的选项可生成单独的文件,但自定义功能有限。
- 自动休息。用于在 npm 包中打包 API 客户端的 npm 解决方案。
- WebApiClientGen。下面作者在此线程中提到的解决方案。
- 打字机。VS 扩展,您可以在其中制作自己的实现。
If you need DTOs only, then I'd look towards
如果您只需要 DTO,那么我会考虑
More on these tools in this post - 6+ ways to marry C# with TypeScript.
在这篇文章中详细介绍了这些工具 -将 C# 与 TypeScript 结合的 6 种以上方法。