Showing C# variables in JavaScript

We need the appSetting values from Web.config inside our application, let’s say we need to console.log() the application version:

<!-- Web.config -->
<configuration>
  <appSettings>
    <add key ="appVersion" value="6.5.10" />
  </appSettings>
</configuration>

You can either define a variable inside a C# code block and reference it in a <scrip> tag or call the value directly inside the <script> block. with @ (e.g. @foo or '@ConfigurationManager.AppSettings["appVersion"]').

You can reference the key value using ConfigurationManager.AppSettings["appVersion"];, and you have to add

<!-- foo.cshtml -->

@using System.Configuration

@{
    var appVersion = ConfigurationManager.AppSettings["appVersion"];
}

<script>
    console.info('app version 2: ' + '@ConfigurationManager.AppSettings["appVersion"]'); // app version 2: 6.5.10
    console.info( 'app version 3: ' + '@appVersion'); // app version 3: 6.5.10
    console.info(`App Version 4: @appVersion`); // App Version 4: 6.5.10
</script>
<!-- foo.cshtml -->

@using System.Configuration

<script>
		let version = '@ConfigurationManager.AppSettings["appVersion"]';

    console.info(`Application Version: ${version}`); // Application version 6.5.10
</script>