maztt
maztt

Reputation: 12294

asp.net mvc c# javascript web.config

i want to have retrieve a "imagetype" from appsettings in my web.config in javascript . how can i do that?

Upvotes: 5

Views: 4462

Answers (2)

Pavel Morshenyuk
Pavel Morshenyuk

Reputation: 11471

You can use following code in your page markup:

<script language="JavaScript" type="text/javascript">
var type = '<%= ConfigurationManager.AppSettings["imagetype"] %>';
</script>

Upvotes: 8

Rob
Rob

Reputation: 45780

Use the following:

var value = System.Configuration.ConfigurationManager.AppSettings["imagetype"];

You may find that for it to work you need to add a reference to System.Configuration.dll if you don't already have one.

Create a new page, and in Page_Load put the line so that it all reads:

Response.Clear();
var value = System.Configuration.ConfigurationManager.AppSettings["imagetype"];
Response.Write(value);
Response.End();

You can now make an AJAX call to the page from Javascript, perhaps using ExtJs and the text will be returned to your javascript.

Alternatively, you could put the following into your page:

<script language="javascript" type="text/javascript">
  var appSettingValue =  '<%=System.Configuration.ConfigurationManager.AppSettings["imagetype"]%>';

  // The variable "appSettingValue" will contain the string from your web.config
  alert(appSettingValue);
</script>

Upvotes: 1

Related Questions