【问题标题】:How to configure the fetch() URL depending on the environment in ASP.NET Core?如何根据 ASP.NET Core 中的环境配置 fetch() URL?
【发布时间】:2021-07-14 13:51:36
【问题描述】:

我有一个包含 2 个项目的 Visual Studio 2019 解决方案:

  1. 一个 ASP.NET Core Web 应用程序(Razor 页面)项目
  2. 一个 ASP.NET Core Web API 项目

我正在使用 Fetch API 从 Razor Pages 应用程序中的 Razor Pages 对 Web API 进行 JavaScript 调用。

例如:

fetch("https://localhost:44325/api/Customer/12")
    .then(response => response.json())
    .then(data => processData(data))
    .catch(err => showError(err));

如何根据 Web 应用运行的环境(即开发(如上)、登台、生产等)配置 url(上面 fetch() 调用的第一个参数)?

【问题讨论】:

    标签: javascript asp.net-core fetch-api


    【解决方案1】:

    如何配置 url(fetch() 调用的第一个参数 以上)取决于Web App运行的环境

    • 即开发(如上)、分期、生产等?

    首先,根据环境添加多个 appsettings.Environment.json :例如 appsettings.Production.json 和 appsettings.Development.json 文件:

    appsettings.Production.json:

    {
      "API": {
        "URL": "https://localhost:44325/api/Customer/12", //store the url
        "Environment": "Production"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      }
    }
    

    然后,在视图页面中,使用@inject 指令添加IConfiguration,然后您可以从视图页面访问配置值。然后你可以使用隐藏字段来存储Api URL,在使用Tetch Api之前,使用Jquery查找隐藏字段并获取URL。

    @inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnv
    
    @inject Microsoft.Extensions.Configuration.IConfiguration configuration
     
    <p> ASPNETCORE_ENVIRONMENT = @hostingEnv.EnvironmentName</p>
     
    <p> ASPNETCORE_ENVIRONMENT = @configuration["API:Environment"]</p>
    <p> API URl : <span>@configuration["API:URL"]</span></p>
    
    <input type="hidden" id="hid_apiurl" value="@configuration["API:URL"]" />
    

    结果如下:

    【讨论】:

      【解决方案2】:

      我正在寻找类似的东西,我找到了这篇文章。 https://www.thecodebuzz.com/set-appsettings-json-dynamically-dev-and-release-environments-asp-net-core/

      您可以拥有多种类型的 appsettings.json 文件,其中包含特定于 DEV、TEST 或 STAGING 和 PROD 的配置详细信息。 然后只需在设置文件中定义您的网址,例如

      appsettings.Development.json

      {    
        "ApiUrls": {
          "CustomerApi": "https://localhost:44325/api/Customer/12",
        }
      }
      

      appsettings.prod.json

        {    
            "ApiUrls": {
              "CustomerApi": "https://productionurlblablabla/api/Customer/12",
            }
          }
      

      等等。那里都描述了它对我有用,但我不是这本书的作者,所以请随意阅读,祝你好运。

      【讨论】:

      • 谢谢@Vladimir - 您能否提供一个代码 sn-p,说明您如何从 JavaScript 代码中访问这些设置?
      • 我在后端站点上调用了我的 api,但我很确定您可以将该值存储在变量中并将其传递到您的视图中。我正在使用 mvc 但我很确定在 razor 页面中你有类似 ViewData["MyApiUrl"] = _configuration.GetValue("ApiUrls:CustomerApi") 之后你可以使用字符串插值或类似的东西将其注入到您的 fetch 脚本中
      • 其中 _configuration 是私有的只读 IConfiguration
      • 你也可以在后端调用你的api,它比在视图中渲染你想要的任何东西更安全
      猜你喜欢
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 2021-12-12
      • 2016-10-06
      • 2018-05-19
      • 2018-11-11
      相关资源
      最近更新 更多