Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

Below code worked for me.

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda