- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个项目:
每个都在两个单独的容器中运行。他们共享一个构建它们的 docker-compose 文件。在 UI 项目中,我希望能够进行 AJAX 调用以命中 API 项目中的端点。 将到达我的端点的 URL 是什么?
我尝试过的:
https://localhost:49239/weatherforecast
成功到达终点。但是,再次强调,我想通过从 UI 项目中的客户端调用它来达到这个端点。https://localhost:49239/weatherforecast
它不会工作。我已尝试对 URL 的主机部分进行多种变体来访问它。Compose sets up a single network for your app. Each container for aservice joins the default network and is both reachable by othercontainers on that network and is discoverable by them at a hostnameidentical to the container name.
为此,我还尝试了多种主机 URL 变体,例如 https://api:49243/weatherforecast
,其中“api”是我的 docker 镜像的名称,“49243”是docker ps
列出的端口。我也试过:
https://api:80/weatherforecast
https://api:433/weatherforecast
https://api:PORT_NUM/weatherforecast
其中“PORT_NUM”是使用 docker ps
那么我该如何到达这个端点呢???
注意:
docker inspect CONTAINER_NAME
,我知道这两个容器都在同一个网络上。文件:
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
<button type="button" class="btn btn-primary" onclick="callAPI()">Call API</button>
</div>
@section scripts {
<script>
function callAPI() {
console.log("calling API...");
$.ajax({
url: `https://api:49221/weatherforast/get`,
method: 'GET',
success: function (data) {
console.log(data);
},
error: function (error) {
console.log(error);
}
});
}
</script>
}
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["UI/UI.csproj", "UI/"]
RUN dotnet restore "UI/UI.csproj"
COPY . .
WORKDIR "/src/UI"
RUN dotnet build "UI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "UI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "UI.dll"]
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace API.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["API/API.csproj", "API/"]
RUN dotnet restore "API/API.csproj"
COPY . .
WORKDIR "/src/API"
RUN dotnet build "API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "API.dll"]
version: '3.4'
services:
ui:
image: ${DOCKER_REGISTRY-}ui
build:
context: .
dockerfile: UI/Dockerfile
api:
image: ${DOCKER_REGISTRY-}api
build:
context: .
dockerfile: API/Dockerfile
最佳答案
最终我发现 Visual Studio 创建了一个我暂时没有注意到的文件,名为 docker-compose.override.yml
。该文件在未指定主机端口的情况下公开了端口 80 和 443,我相信这是导致我的应用程序每次运行时都以随机端口号运行的原因。这让我很难知道将我的 API 调用指向何处。
本质上,只需将端口添加到我的 docker-compose.yml
(如下面的代码块所示)并且(重要的是)从 docker-compose.override.yml
我能够让 UI 和 WebAPI 应用程序分别在指定端口上运行。从那里,我需要将主机名 “https://localhost:49254”
添加到我的 WebAPI 应用程序中的 CORS 策略,这样它就不会阻止来 self 的 UI 应用程序的请求。
version: '3.4'
services:
ui:
container_name: ui_container
image: ${DOCKER_REGISTRY-}ui
build:
context: .
dockerfile: UI/Dockerfile
ports:
- "49254:443"
api:
container_name: api_container
image: ${DOCKER_REGISTRY-}api
build:
context: .
dockerfile: API/Dockerfile
ports:
- "49255:443"
因此,要访问我的 API 端点,我必须使用 ajax 访问 https://localhost:49255/WeatherForecast
。
要查看我的所有代码,请查看我的 GitHub repository .
显然,如果我要将此应用程序投入生产,我需要调整主机名。
如果有人知道更好/更清洁的解决方案,请提出建议。
关于docker - 如何从单独的 Docker 容器中调用 ASP.NET Core Web API 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65673220/
Java 专家需要您的帮助。 今天我在一次采访中被问到这个问题,但我无法解决。所以我需要一个解决方案来解决这个问题; 反转字符串 Input : Hello, World! Output : oll
目标:单击按钮并将成分作为单独的项目添加到数组中。 当前设置: 这给出:蓝莓芒果柠檬汁 然后我希望能够通过单击按钮将成分作为单独的项目添加到数组中: var allI
如何编写正则表达式来匹配它(参见箭头): "this is a ->'' this is a "test' there is another "test' 第二种情况 /\b'/ Regex Demo
我有一个数组,其中包含有限数量的项目。我想随机删除项目,直到所有项目都被使用过一次。 示例 [1,2,3,4,5] 使用了随机数 5,所以我不想再这样了。使用了随机数 2,所以我不想再这样了。等等..
首先,抱歉,如果这太主观了,我只是不知道还能怎么问/去哪里问。 无论如何,鉴于我最近的所有问题,我准备很快发布一个 Android 应用程序,并且大部分测试都是在我的手机 Droid 上完成的。我真的
这可能不是这个问题的正确位置,如果不合适请随意移动它。我标记为 Delphi/Pascal 因为这是我在 atm 中工作的内容,但这可能适用于我猜的所有编程。 无论如何,我正在做一些代码清理,并考虑将
我像这样分隔了其余 api 的路由。有没有更好的方法来组织路由器?还是我现在的做法没问题? app.js app.use('/api/auth',auth); 应用程序/ Controller /au
我在 2 个单独的工作表中包含以下数据: 表1: A B C D a ff dd ff ee b 12 10 10 12 表2: A B C
我正在使用 jQuery,并在位于单独 HTML 文件中的表中获取了几行。单击时,每一行都会成功重定向到本地 HTML 文件。 (使用window.location) 我想要实现的目标 我想要完成的是
我有重叠背景图像的问题,当它们重叠时会导致阴影比不重叠时更暗,从而产生不均匀的阴影。 我有一个高度灵活的盒子,带有一些透明的背景图像和阴影以创建漂亮的边框。盒子本质上是 3 个元素。 您可以在此处找到
按照正常的微服务框架,我们希望将每个微服务放入其自己的 git 存储库中,然后为 Service Fabric 项目创建一个存储库。当我们更新其中一个微服务时,Service Fabric 项目将仅重
我想将多个片段嵌入到一个指令中。这是我的设置方式。 Everyone Development (3)
我希望在保留原件的同时将多个文件 gzip 到一个目录中(到多个 .gz 文件中)。 我可以使用这些命令来处理单个文件: find . -type f -name "*cache.html" -exe
有没有办法分别知道每个 Eclipse 插件消耗了多少内存? 最佳答案 进行堆转储并使用例如分析它Eclipse Memory Analyser . 如需更多信息,请参阅 Analyzing Equi
我们使用cusrom插件并以这种方式定义脚本(这是一个近似的伪代码): //It is common part for every script (1) environments { "env1"
我在控制台应用程序中托管了一个集线器,并有一个 WPF 应用程序连接到它。它工作得很好。然后我将集线器移到一个单独的项目中,并将主机的引用添加到新项目中。现在我收到 500 错误,没有其他详细信息。
是否可以在单独的 JAR 文件中为 JavaBean 构建类?具体来说,JavaBean 在一个 JAR 文件中具有 Bean 和 BeanInfo 类,而自定义属性编辑器类位于另一个 JAR 文件中
好的,所以我有一个 MAF 应用程序,它在单独的应用程序域中加载每个插件。这非常适合我的需要,因为它允许我在运行时动态卸载和重新加载我的插件。 问题是,我需要能够在子应用域中处理未处理的异常,捕获它,
在参加在线数据库类(class)(针对初学者)时,我注意到一个问题,我必须查找涉及...至少两个不同值的查询...例如, ELMASRI 书中的 COMPANY 数据库指出:查找至少从事两个不同项目的
(首先:我已经尝试了涉及边距、边框等的所有选项。) Link to problematic page. Link to similarly constructed, non-problematic p
我是一名优秀的程序员,十分优秀!