javascriptc#asp.netknockout.jsknockout-mvc

howto retrieve asp.net appSettings key from front-end .js file using knockout.js


I have an asp.net backend application and i am using web.config and other files to store configuration keys.

I have a front-end built with javascript files using knockout.js.

We would like to know how can we retrieve key value from web.config in backend and read this value in front-end using javascript and knockout.js.

Is there a simple way to do this ???, Views are javascript files and not asp.net web pages


Solution

  • For Razor .cshtml in ASP.NET MVC and ASP.NET 4.x WebPages:

    (I assume your <head> is in _Layout.cshtml)

    @using System.Configuration
    @using System.Web.Configuration
    
    <html>
    <head>
        <script>
    
    var myAppSetting = '@( HttpUtility.JavaScriptStringEncode( WebConfigurationManager.AppSettings["myAppSetting"], addDoubleQuotes: false ) )';
    
        </script>
    </head>
    <body>
    </body>
    

    For ASP.NET WebForms .aspx/.ascx/.master and/or ASP.NET MVC 1.x and 2.x using the WebForms ASPX View Engine:

    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="System.Web.Configuration" %>
    <html>
    <head>
        <script>
    
    var myAppSetting = '<%= HttpUtility.JavaScriptStringEncode( WebConfigurationManager.AppSettings["myAppSetting"], addDoubleQuotes: false ) %>';
    
        </script>
    </head>
    <body>
    </body>
    

    For ASP.NET Core MVC's Razor .cshtml views:

    @using System.Text.Encodings.Web
    @inject IConfiguration Config
    
    <html>
    <head>
        <script>
    
    var myAppSetting = '@Html.Raw( JavaScriptEncoder.Default.Encode( this.Config.GetValue<String>("appSettings:myAppSetting") )';
    
        </script>
    </head>
    <body>
    </body>
    

    Note that if you want null values from your appSettings.json to appear as JavaScript null literals then you need to do that manually - you can use a @functions method to handle this, like so:

    @using System.Text.Encodings.Web
    @using Microsoft.AspNetCore.Html
    @inject IConfiguration Config
    @functions{
        
        public HtmlString GetAppSettingJSString( String name )
        {
            if( name is null ) throw new ArgumentNullException(nameof(name));
    
            String? appSettingValue = this.Config.GetValue<String?>( "appSettings:" + name );
            return ToJSStringLiteral( appSettingValue );
        }
    
        private static readonly HtmlString _nullLiteral = new HtmlString( "null" );
    
        public static HtmlString ToJSStringLiteral( String? value )
        {
            if( value is null ) return _nullLiteral;
            
            String jsStringLiteralChars = JavaScriptEncoder.Default.Encode( value );
            return new HtmlString( '"' + jsStringLiteralChars + '"' );
        }
    }
    
    <html>
    <head>
        <script>
    var myAppSetting = @Html.Raw( this.GetAppSettingJSString( "myAppSetting" ) );
        </script>
    </head>
    <body>
    </body>
    

    So now the <script> above will be rendered as either this:

    <script>
    var myAppSetting = "foobarbaz";
    </script>
    

    ...or this:

    <script>
    var myAppSetting = null;
    </script>
    

    Any script (except ES Modules, I think?) can access myAppSetting via the implicit global (aka window) object, as all top-level var declarations become global properties.

    So like so:

    <script>
    document.addEventListener( 'DOMContentLoaded', function() {
    
        alert( "myAppSetting: \"" + window.myAppSetting + "\"" );
    
    } );
    </script>