简体   繁体   English

将 Appsettings.json (.core) 中的 url 参数传递给 React

[英]Pass url parameter from Appsettings.json (.core) to React

I am trying to access the url parameter inside the appsettings.json file.我正在尝试访问 appsettings.json 文件中的 url 参数。 The reason is that the api URL differs from Development to publish.原因是 api URL 不同于开发发布。 I am not entirely sure the best way to solve this.我不完全确定解决此问题的最佳方法。

I've found a solution that could work:我找到了一个可行的解决方案:

how to get config data from appsettings.json with asp.net core & react 如何使用 asp.net 核心从 appsettings.json 获取配置数据并做出反应

As I've understood from above thread, I need to create a service and call it from React?正如我从上面的线程中了解到的,我需要创建一个服务并从 React 调用它? This seems abit wierd since I always(?) need to do a API request to the same project to recieve the URL which is needed for the API requests.这似乎有点奇怪,因为我总是(?)需要向同一个项目发出 API 请求以接收 ZDB974238714CA8DE634A7CE1D083A14F 请求所需的 URL。

The other suggestion is to use webpack, or somehow save the url in the clientside.另一个建议是使用 webpack,或者以某种方式将 url 保存在客户端。 But won't this mean that whenever I need to change environment, I need to change that in 2 places (backend & frontend)?但这是否意味着每当我需要更改环境时,我需要在 2 个地方(后端和前端)进行更改?

This is how my appsettings.json file looks (same variable but different values for.Development and.Publish).这就是我的 appsettings.json 文件的外观(相同的变量,但.Development 和.Publish 的值不同)。

{
    "Url": "localhost:44000"
}

In my Startup class I am getting the value:在我的启动 class 中,我得到了值:

    var urlValue =
        _configuration.GetSection("Url");

Can't I somehow get the value once depending on environment from the backend and recieve it using React?我不能根据后端的环境以某种方式获得价值并使用 React 接收它吗?

Not sure if I am thinking wrong here?不确定我是否在这里想错了?

Would appreciate if anyone is able to point me to the right direction.如果有人能够指出我正确的方向,将不胜感激。

My solution to this is using.env documents but still using.core launchSettings env.我对此的解决方案是使用 .env 文档,但仍使用 .core launchSettings env。

For this solution to work, I was in need of env-cmd:为了使这个解决方案起作用,我需要 env-cmd:

npm install env-cmd

Added 3.env files:添加了 3.env 文件:

 1. .env
 2. .env.development
 3. .env.prod

Each file contains a url string:每个文件包含一个 url 字符串:

REACT_APP_Url = https://localhost:44000

I added env check in.core startup file:我添加了 env check in.core 启动文件:

    app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseReactDevelopmentServer(npmScript: "start");
                }
                else if (env.IsProduction())
                {
                    spa.UseReactDevelopmentServer(npmScript: "build");
                }
            });

When I am running production code - I am refering to "build", so in my case the object "build" is going to run.当我运行生产代码时 - 我指的是“构建”,所以在我的情况下,object“构建”将运行。

package.json file: package.json 文件:

"scripts": {
    "start": "rimraf ./build && react-scripts start",
    "build": "cross-env env-cmd -f .env.prod react-scripts start",
  }

I can now access the url by using: {process.env.REACT_APP_Url}我现在可以使用以下命令访问 url: {process.env.REACT_APP_Url}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将ConnectionString从appsettings.json文件传递到dot net core 2.2中的类库项目 - How to pass ConnectionString from appsettings.json file to class library project in dot net core 2.2 如何使用asp.net core从appsettings.json获取配置数据并做出反应 - how to get config data from appsettings.json with asp.net core & react 从.NET Core 2中的类读取appsettings.json - Read appsettings.json from a class in .NET Core 2 从 .net core 3.1 中的 appsettings.json 获取值 - Get values from appsettings.json in .net core 3.1 如何从 .NET 核心中的 appsettings.json 读取部分值 - How to read section values from appsettings.json in .NET Core 模型 .net 核心中 appsettings.json 的值 - Value from appsettings.json in model .net core 如何在Entity Framework Core中从appsettings.json设置连接字符串 - How to set connectionstring from appsettings.json in Entity Framework Core 从 .net 核心中的 appsettings.json 获取价值 - Getting value from appsettings.json in .net core 在.net核心的单元测试中使用和注入appsettings.json - Using and injecting from appsettings.json in unit tests on .net core .Net Core Cant从appsettings.json中读取连接字符串 - .Net Core Cant read connection string from appsettings.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM