我如何用blazor(c#)代码连接到c#web api,两者都运行在gitpod上?

我如何用blazor(c#)代码连接到c#web api,两者都运行在gitpod上?,c#,blazor,gitpod,C#,Blazor,Gitpod,我目前正在开发一个PoC(概念验证)应用程序,前端是Blazor,后端是用于数据访问的c#web api 每次我想访问api时,我都会收到被拒绝的错误连接。 我确实有点奇怪的设置 我使用gitpod进行开发(在线IDE,visual studio代码在线) 整个过程都在虚拟Ubuntu服务器上运行 文件夹结构为: 布拉佐波克 原料药 控制器 ApiRunningController.cs 布拉佐拉普 页数 剃刀索引 我需要从Index.razor调用api。 我这样调用API: prote

我目前正在开发一个PoC(概念验证)应用程序,前端是Blazor,后端是用于数据访问的c#web api

每次我想访问api时,我都会收到被拒绝的错误连接。 我确实有点奇怪的设置

  • 我使用gitpod进行开发(在线IDE,visual studio代码在线)
  • 整个过程都在虚拟Ubuntu服务器上运行
  • 文件夹结构为:

    布拉佐波克

    原料药

    控制器

    ApiRunningController.cs

    布拉佐拉普

    页数

    剃刀索引

我需要从Index.razor调用api。 我这样调用API:

protected override async Task OnInitializedAsync()
{
    try
    {
        status = await Http.GetJsonAsync<string>("https://localhost:8394/ApiRunningController");
    }
    catch(Exception e)
    {
        requestSuccess = false;
        status = "ERROR: " + e.Message;
    }
 }
Startup.cs中的My ConfigureServices()和Configure()方法:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddCors();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

        app.UseCors(c => c
               .AllowAnyOrigin()
               .AllowAnyMethod()
               .AllowAnyHeader()
           );
    }
Api控制器:

[ApiController]
[Route("[controller]")]
public class ApiRunningController : ControllerBase
{
    // Some other code like initializing a logger in the constructor etc...
    [HttpGet]
    public string Get()
    {
        ApiModel model = new ApiModel();
        return model.Status;
    }
}

我想我已经发布了很多重要的代码片段。如果我忘了什么,不要介意留下评论。我会尽快发布。

用户解决方案:sven efftinge

必须在控制台中执行“gp url 8394”以获取端口8394的翻译url。然后,我不得不将该url用于HttpRequest

protected override async Task OnInitializedAsync()
{
    try
    {
        string translated_url = "https://8394-...gitpod.io/ApiRunningController"
        status = await Http.GetJsonAsync<string>(translated_url);
    }
    catch(Exception e)
    {
        requestSuccess = false;
        status = "ERROR: " + e.Message;
    }
 }
受保护的重写异步任务OnInitializedAsync()
{
尝试
{
字符串转换\u url=”https://8394-...gitpod.io/ApiRunningController"
status=wait Http.GetJsonAsync(已翻译的url);
}
捕获(例外e)
{
requestSuccess=false;
status=“ERROR:”+e.消息;
}
}

您是否尝试在浏览器中调用api?您的url应该是:
https://localhost:8394/ApiRunning
如果要从外部访问gitpod开发环境中公开的端口,需要将localhost URL转换为公共URL。在终端上运行
gp url
,以获取外部url。看见
protected override async Task OnInitializedAsync()
{
    try
    {
        string translated_url = "https://8394-...gitpod.io/ApiRunningController"
        status = await Http.GetJsonAsync<string>(translated_url);
    }
    catch(Exception e)
    {
        requestSuccess = false;
        status = "ERROR: " + e.Message;
    }
 }