将Swagger UI添加到自定义.NET Web API [英] Add Swagger UI to a custom .NET Web API
问题描述
我想将Swagger UI集成到我的C#.Net Web API项目中.这与传统的Web API项目不同,在传统的Web API项目中,您可以安装SwashBuckle之类的工具,然后该工具将提取在控制器类中定义的HTTP路由并创建UI.
I want to integrate Swagger UI into my C# .Net Web API project. This is different from a traditional Web API project where you can install a tool like SwashBuckle, which then picks up the HTTP routes defined in your controller classes and creates the UI.
我拥有的是一个自定义实现,可以为不同的路由生成OpenAPI/Swagger 3.0 yaml文件.
What I have is a custom implementation which generates the OpenAPI/Swagger 3.0 yaml files for different routes.
我在通用控制器文件中定义了一个路由,该路由接受一个变量-
I have a route defined in a generic controller file which accepts a variable -
http://localhost:8000/myapp/swagger/{document_name}
其中document_name是变量.我的代码使用了此变量,并根据该值生成了自定义文档.
where document_name is a variable. My code takes this variable and generates the custom documentation based on this value.
http://localhost:8000/myapp/swagger/sedan
http://localhost:8000/myapp/swagger/suv
在上面的示例中,我的代码将为轿车和suv返回不同的Open API规范.
In the example above my code would return a different Open API spec for sedan and suv.
到目前为止,URL仅以纯文本形式返回Open API 3.0 YAML规范.我想要做的就是能够在Swagger UI中表示与此文件类似的文件-
As of now the URL simply returns Open API 3.0 YAML spec in plain text. What I want to be able to do is to be able to have this file represented in the Swagger UI similar to this -
这可能吗?我可以使用Swashbuckle吗?我是否需要做一个自定义实现来托管Swagger UI?最好的方法是什么?
Is this possible? Can I use Swashbuckle for this? Do I need to do a custom implementation to host the Swagger UI? What's the best way?
推荐答案
如果您已经拥有OpenAPI/Swagger 3.0 Yaml文件,则您需要的只是UI,并且有一些选择:
If you already have the OpenAPI/Swagger 3.0 yaml file(s) all you need is the UI and you have a few options:
-
将UI文件复制到您的服务器.
这将为您提供最大的灵活性,允许您使用 https://github.com/swagger-api/swagger-ui/树/主/dist
使用petstore或任何其他现有的UI并将您的文件作为参数传递,如下所示:
http://petstore.swagger.io/?url = https://raw.githack.com/heldersepu/hs-scripts/master/swagger/swagger_aws.json
Use the petstore or any other existing UI and pass your file as parameter, like this:
http://petstore.swagger.io/?url=https://raw.githack.com/heldersepu/hs-scripts/master/swagger/swagger_aws.json
这篇关于将Swagger UI添加到自定义.NET Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!