我可以让 Swagger 界面出现在已部署的 Azure Web API 上吗?

发布于 2025-01-14 16:32:12 字数 477 浏览 2 评论 0原文

当人们在 Visual Studio 2022 中创建 ASP.NET Core Web API 并在本地测试它时,人们会得到一个基于 OpenAPI 定义构建的方便的 Swagger 页面,以测试所有 HTTP 端点。

但是,当部署并尝试访问 {path-to-api}/swagger 时,即使在 localhost 上,它也会返回 404 Not Found 错误>,当 API 和数据库都位于我自己的机器上时。即使数据库位于 Azure 云中,如果我将 Azure SQL 数据库连接字符串放入 appsettings.json,它也可以工作。

那么有没有一种方法可以实现这一点,最好没有太多麻烦呢?或者我想要这个是错误的,开发人员大多在本地测试他们的 API 吗?因为我希望 Swagger API 在线仅用于测试。


问题是将 swagger 功能获取并使用到云中。这是可能的并且是好的做法吗?

When one creates an ASP.NET Core Web API in Visual Studio 2022, and tests it locally, one gets a convenient Swagger page built upon an OpenAPI definition, to test all HTTP endpoints.

However, when deployed and trying to access {path-to-api}/swagger, it returns a 404 Not Found error, even while on localhost, when both the API and the database is sitting on my own machine. Even if the database is in the Azure cloud, for that matter, it also works, if I put the Azure SQL Database connection string into appsettings.json.

So is there a way to achieve this, preferably without too much hassle? Or am I wrong in wanting this, do developers mostly test their APIs locally? Because I want the Swagger API online only for testing.


The problem is getting and using the swagger functionality into the cloud. Is it possible and good practice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在风中等你 2025-01-21 16:32:12

如果您查看启动,您会发现 swagger 仅在开发会话期间通过 if 检查加载。注释掉它,或者根据环境对其进行扩展,将允许发布的版本在目标主机上生成页面。

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

我通常在首次发布或开发/测试环境中执行此操作以查看其运行情况。一旦不需要,我就会取消注释。

此外,它在开发或 UAT 服务器中实际上可能是可行的(打开),因为其中一个也在发布开放 API 它到 APIM(Azure api 管理器),它获取 api 并生成自己的开发环境;远离最初的发布。


此外,一旦发布,它就不是默认页面,仍然需要指向它的路径,例如 .../swagger/index.html

If you look at the startup, you will notice that the swagger is only loaded during a development session via an if check. Commenting that out, or expanding it based on evironment, will allow a published version to generate the page on the target host.

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

I generally do that for first publishes or to Dev/Test environments to see it running. Once it is not needed, I un-comment it back in.

Also it may be actually viable (turned on) in Dev or UAT server because one is also publishing the open api it to APIM (Azure api manager), which takes the api and generates its own development environment; away from an initial publish.


Also once published, it is not the default page, one still has to path to it such as .../swagger/index.html.

过期情话 2025-01-21 16:32:12

我将中止将 Swagger 接口与我的 API 一起部署到 Azure 的任务。让所有人都可以直观地看到 HTTP 请求方法是很糟糕的安全实践。因此,我的问题开发人员是否主要在本地测试他们的 API 的答案显然是肯定的。

我想知道是否应该删除这个问题,但我想让它仍然存在,以防其他人正在考虑做同样的事情 - 使用 Swagger UI 在线公开 API。

I'm aborting this mission to deploy the Swagger interface to Azure along with my API. It's bad security practice to make the HTTP request methods so visually available to all. So the answer to my question do developers mostly test their APIs locally, is apparently yes.

I wondered if I should remove the question, but I would like to make it still stand, in case anyone else is contemplating about doing the same thing - exposing an API online with the Swagger UI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文