16

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?

1

7 Answers 7

18

"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.

Sign up to request clarification or add additional context in comments.

Comments

16

Below code worked for me.

<script>
    var apiUrl = '@System.Configuration.ConfigurationManager.AppSettings["url"]';
</script>

Comments

12

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()';

Comments

2

Below code perfectly worked for me. I think you are missing namespace.

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

1 Comment

I really wonder if the solution mentioned above worked for any one, the correct answer should be what @darson1991 mentioned. which is <script> var apiUrl = '@System.Configuration.ConfigurationManager.AppSettings["url"]'; </script>
1

index.html

<input id="getImageUrl" value="@System.Configuration.ConfigurationManager.AppSettings["OPMGTWebUrl"]" style="display:none" />

index.js

var imageUrl = document.getElementById("getImageUrl").value;

1 Comment

Hi and welcome to stackoverflow, and thank you for answering. While this code might answer the question, can you consider adding some explanation for what the problem was you solved, and how you solved it? This will help future readers to understand your answer better and learn from it.
0

Complementing freedomn-m's answer, doesn't seem possible to do it from within the js file directly.

So my solution was to access the configuration from the cshtml and pass it to the js script as a parameter.

I used the first method here to pass arguments

So my code looked something like this:

My cshtml file

<script id='my-unique-id'
        my-arg='@System.Configuration.ConfigurationManager.AppSettings["MY_KEY"]'>
        src='test.js'
</script>

My 'test.js' file

const myArg = document.GetElementById('my-unique-id').getAttribute('my-arg');

Comments

-3

The following line will return the URL value.

var apiUrl = '<%=ConfigurationManager.AppSettings["url"]%>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.