Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
How can I read key value from web.config in javascript file?

I am trying to read apiUrl key value from web.config file so that I can take advantage of the .net transform config to manage deployment in different environments. Here is my Webconfig code:

<appSettings>
    <add key="url" value="http://localhost:6299/api/"  
</appSettings>

and in the plain js file I have this code:

var apiUrl = '<%=ConfigurationManager.AppSettings["url"].Tostring()
%>'.

It is not giving the url value. How can I read web.config value in javascript file?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

"In the plain js file"

do you mean a file ending in .js ?

.js files are not parsed server-side so the <%= values are not converted. This works for the other answer ("worked for me") as they will have it in the .aspx/.cshtml file rather than a 'plain .js file'.

You'll need to move your code to your .aspx/.cshtml or you'll need to pass the url value in to your js (eg) via a function parameter from the .aspx/.cshtml file.

over 4 years ago · Santiago Trujillo Report

0

Below code worked for me.

<script>
    var apiUrl = '@System.Configuration.ConfigurationManager.AppSettings["url"]';
</script>
over 4 years ago · Santiago Trujillo Report

0

Below code worked for me in ASP.Net webforms application, but not in MVC application

var key = '<%= System.Configuration.ConfigurationManager.AppSettings["key"].ToString() %>';

for MVC application in .cshtml page try below

 var key = '@System.Configuration.ConfigurationManager.AppSettings["key"].ToString()';
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!