如何从TypeScript中的web.config获取密钥? [英] How to get key from web.config in TypeScript?

查看:98
本文介绍了如何从TypeScript中的web.config获取密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将API项目中的web.config中的密钥获取到TypeScript UI项目中?我尝试了以下操作,但是没有任何访问权限可以告诉我的配置文件:

How can i get the a key from web.config in API project into my TypeScript UI project? I've tried the following but nothing gets access to the configuration file that i can tell:

 apiUrl: string = '@System.Configuration.ConfigurationManager.AppSettings["ApiUrl"]';

这也不起作用:

 apiUrl: string = "@ConfigurationManager.AppSettings['ApiUrl']";

我也尝试了所有引号组合,无论是单引号还是双引号,都无济于事.关于如何从TypeScript中的web.config文件获取值的任何见解?

I've also tried every combination of quotes whether single or double, to no avail. Any insight as how to get a value from the web.config file in TypeScript?

推荐答案

尝试从其他程序集/项目访问配置文件通常不是一个好主意,因为编译时它们通常不会输出到同一目录.

It's generally not a good idea to try accessing configuration files from other assemblies/projects, as they are typically not output to the same directory when compiled.

由于TypeScript已转换为JavaScript,因此无论驻留在何处,您都无法直接从配置文件中读取.

Because TypeScript is transpiled into JavaScript, you cannot read directly from a configuration file, regardless of where it resides.

如果您要动态更改服务的URL(例如,从DEV端点更改为QA或Production端点),则可以在HTML/CSHTML文件中使用 Razor语法来获取值从配置文件中,然后将值注入到脚本中, OR ,您可以在部署过程中使用 Powershell 替换TypeScript文件中的值在构建过程中.

If you want to dynamically change URLs for a service (say from a DEV endpoint to a QA or Production endpoint), you can either use Razor Syntax in your HTML/CSHTML file to grab the value from the configuration file, and then inject the value into the scripts, OR you could use Powershell during deployment to replace the values in your TypeScript files during the build process.

# Argument passed into PowerShell script (or write out URL string manually).
$NewURL1 = $args[0];
$NewURL2 = "http://goodURL.com/api";

# *.js files, because your *.ts files will already be transpiled.
$files = gci "C:\MyProject\DropFolder" -recurse -include "exampleWithBadURL.js", "otherFile.js";

Foreach ($file in $files)
{
    $fileContent = Get-Content($file) -Raw;
    attrib $file -r;

    # Perform the replacement.
    $fileContent -replace "http://localhost:38774/", $NewURL1 | Out-File $file;
    $fileContent -replace "http://badURL.com/api/", $NewURL2 | Out-File $file;

    Write-Output($file.FullName);
}

如果构建/发布代理允许,则在将代码部署到特定环境时可以运行此Powershell脚本.复制文件后,在发布定义中,我在 VSTS/TFS 中使用了类似的脚本.

If your build/release agent allows it, you can run this Powershell script when deploying your code to a specific environment. I use a similar script in VSTS/TFS in my release definition after I copy the files over.

这篇关于如何从TypeScript中的web.config获取密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆