How to get the key from web.config in TypeScript?

How can I get the key from the web.config project in an API in a TypeScript UI project? I tried the following but get nothing access to the configuration file, which I can say:

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

This also does not work:

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

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

+5
source share
1 answer

As a rule, it is not recommended to try to access configuration files from other assemblies / projects, since they usually are not output to the same directory during compilation.

Since TypeScript translates to JavaScript, you cannot read directly from the configuration file, no matter where it is located.

If you want to dynamically change the URLs for a service (say, from a DEV endpoint to a production checkpoint or endpoint), you can use the Razor Syntax in your HTML / CSHTML file to capture the value from the configuration file, and then enter the value in scripts OR , you can 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); } 

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

0
source

All Articles