In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Net Core WebAPI to Swagger under how to add export offline document function, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
First, preface
I recently came into contact with Swagger and downloaded its source code and demo on github. I found that this component is very easy to use, but the deficiency is that it does not have the ability to export offline documents, so I want to add an export function to it.
Swagger Github open source address
Second, train of thought
In fact, to put it bluntly, the api document is a html static page, html can be transferred to word or pdf, then the problem becomes simple.
1. Find a way to get the api interface json data generated by swagger.
two。 Convert json to html.
3. Convert html to files such as word or pdf.
Third, realization
1. First of all, we need to solve the first problem. To add an export button to the Swagger page, Swagger provides an interface. We can use this interface to inject js and css into the implementation, as shown in the figure:
The effect is as follows:
two。 After getting the document data generated by swagger, I looked up the source code on swagger github and found the code he used to generate the document, as shown below:
two。 Then we use net core's ioc container injection to inject this object into the ioc container for later use, as shown in the figure:
3. Take the object out of the ioc container, convert the json object to html, and then convert the html into a file that needs to be exported.
As shown in the figure:
Here is a RazorEngine, which is a template engine packaged by Microsoft. The most important point is that it can be used without mvc and can be used to customize its own code generator. I personally think it is easier to use than T4. Complain that the syntax of T4 looks really awkward. And look at the RazorEngine grammar. It looks much more comfortable.
Are you familiar with it?
Final result:
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.