In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you. NetCore2.1 WebAPI according to swagger.json automatically generate client code is how, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
Preface
Through the Swagger plug-in, it is very convenient to provide interface developers with online debugging, but in fact, there are many functions that come with Swagger.
For example, use NSwag to generate client call code to further liberate interface developers.
NSwag
NSwag is an open source project released on GitHub that can generate C # and TypeScript client code based on the swagger.json file on the Swagger description page.
Project address of NSwag: https://github.com/RSuter/NSwag
Nswag provides four code generation methods
1. Use NSwagStudio, a Windows desktop application used to generate client-side code for API in C # and TypeScript.
2. Use NSwag.CodeGeneration.CSharp or NSwag.CodeGeneration.TypeScript NuGet packages to perform code generation in the project.
3. Use NSwag on the command line.
4. Use NSwag.MSBuild NuGet package.
NSwagStudio is recommended here. You can download the tool from GitHub at https://github.com/RSuter/NSwag/wiki/NSwagStudio
Download and install. Open NSwagStudio after installation, as shown in the following figure, select Documents from the menu of Tab page on the left.
For example, we need to pay attention to the points selected in the box above. Where Swagger Specification URL is the online address of swagger.json of our WebAPI.
If your WebAPI is not online when you click the [Create local Copy] button, the code generation tool will pop up an error dialog, as shown below:
So the prerequisite for generating code by reading Swagger Specification URL is that you must make sure that swagger.json files can be read online!
Second, you can choose RunTime (runtime), which should be the runtime of the server-side WebAPI (after all, TypeScript doesn't care whether your server is the .NET Core or the .NET Framework).
Because my environment is .NET Core2.1, I choose NET Core21 here.
Next, click the "Create local Copy" button. After clicking, NSwagStudio will interact with the WebAPI server. After success, the swagger.json file will be formatted into the text editor on the left, as shown below:
At this point, you can check the file format you want to output in the Outputs on the right. Here I choose TypeScript and CSharp Client.
There is a C # WebAPI Controller in this place. I'm a little confused. I have swagger.json files. The cliff already exists webapi.
There is no need to reverse the generation of webapi's controller again. I don't understand. Just generate the client code anyway.
After we have checked the box, the following output matching page will appear, as shown below:
We choose the CSharp Client page, which is divided into Setting and Output pages on the left. The Setting page can configure the output cs file, such as namespace, class name, output file path, etc. (I don't know many configurations either.)
After we click "Generate Outputs", NSwagStudio will generate the client operation class according to the configuration, which can be checked on the Output page. After checking the error, click "Generate Files" to export the class file to the output directory of the configuration.
The same is true of TypeScript. At the same time, NSwagStudio can also support loading DLL reflection generation, which can be done according to the official website. (after all, you can directly use json files to generate online. There is no need to manually select dll...)
Here we take a screenshot to see the SwagerUI page and the client cs file generated by NSwagStudio
The above is how NetCore 2.1 WebAPI automatically generates client code based on swagger.json. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.