.Net Core 2.1 - Read appsettings.json in javascript file

get value from appsettings json c# .net core console
net core read appsettings json
asp.net core read appsettings.json from class library
how to get appsettings value in javascript
get connection string from appsettings json net core
update appsettings.json programmatically c#
asp.net core read json file
appsettings json list of objects

I have a Web application in .net Core 2.1 using signalR. I need to pass HubUrl into custom javascript file. Is that possible in .net Core?

Sample of js code:

const connection = new signalR.HubConnectionBuilder()
.withUrl('http://localhost:5000/hub') //Here I need to read appSettings.json to get value from there
.configureLogging(signalR.LogLevel.Information)
.build();

appsettings.json located on server. So you need to add end-point to controller that returns needed value.

Controller:

public class MyController:Controller{
    private readonly IConfiguration configuration;

    public MyController(IConfiguration configuration){
         this.configuration = configuration;
    }

    [HTTPGet]
    public ActionResult GetConfigurationValue(string sectionName, string paramName){
        var parameterValue= configuration[$"{sectionName}:{paramName}"];
        return Json(new { parameter= parameterValue});
    }
}

Client side:

$.ajax({
    type:"GET",
    url: "/MyController/GetConfigurationValue"
    data:{
        sectionName = "MySection",
        paramName = "MyParameter"
    }
}).done(
    function(parameterValue){
        //do what you need
});

In appsettings.json:

{
    "MySection":{
        "MyParameter":"value that I want to get"
    }
}

Reading Values From Appsettings.json In ASP.NET Core, I have a Web application in .net Core 2.1 using signalR. I need to pass HubUrl into custom javascript file. Is that possible in .net Core? Sample of js code: Let’s see the demo of how to set values and read values from "appsettings.json" file. For doing this, let's create a .NET Core web application first. From Visual IDE, choose File Menu >> New >> Project. After choosing the Project menu, a new dialog will pop up with the name “New Project”, as shown below.

A very safe yet flexible way to do this is to expose the URL inside a <script> block in a view. That way, you are not exposing application configuration (which access you will have to protect very well) but instead just explicitly expose a single secure value.

It also has the added benefit that you do not need to make a request to an endpoint (which address you actually also need to know in your script) but have the value instantly there when you need it.

To do that, you can simply render a <script> tag that defines the value globally. For example, you could have the following in your layout:

<script>
window._signalRAddress = @Json.Serialize(ViewData["SignalRAddress"]);
</script>
<script src="~/your-custom-script.js"></script>

And then you just use window._signalRAddress inside of your script.

Setting And Reading Values From "appsettings.json" In .NET Core, config file, but in ASP.NET Core application, there is no such thing as web.config. So, how do we do it? There is a file present for this purpose only  There are multiple ways to read value from Appsettings.json file First we learn simple way, like how we used to read value from web.config appsettings section. In Asp.net Core we need to refer a namespace Microsoft.Extensions.Configuration; and then use IConfiguration to read the value from appsettings.json file.

In an SPA with webpack, you can just import the appsettings.json and make it available via a convenience class that you import;

e.g.

appsettings.js:

appsettingsjson = process.env.NODE_ENV === 'development'
    ? require('./appsettings.Development.json')
    : require('./appsettings.json');

class AppConfig {

    constructor() {
    }

    static Settings() {
        return appsettingsjson;
    }
}

module.exports = AppConfig;

Then wherever you need it, just import and use. e.g.:

const AppConfig = require("./appsettings");
console.log(AppConfig.Settings().YourSection.YourParameter)

ASP.NET Core: Application Settings in Razor, The "appsettings.json" file contains configuration settings. This file is similar NET Core, the old style of reading values doesn't work here. Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. In ASP.NET MVC, we used to keep such information in web.config file, but in ASP.NET Core application, there is no such thing as web.config. So, how do we do it? There is a file present for this purpose only which is called appsettings.json.

respecting "Maxemelian"'s answer, extending answer by adding validation cases to check

  1. If section name is null
  2. Ignoring secret
private readonly IConfiguration configuration;

public MyController(IConfiguration configuration){
     this.configuration = configuration;
}

[HttpGet("[action]")]
public ActionResult GetConfigurationValue(string sectionName, string paramName)
{
    if (paramName.ToLower().Contains("secret")) {
        return null;
    }

    var parameterValue = (!string.IsNullOrEmpty(sectionName)) ? config[$"{sectionName}:{paramName}"] : config[$"{paramName}"];
    return Json(new { parameter = parameterValue });
}

ASP NET Core appsettings json file, This article provides code that works for dotnet version 2.1.4. While trying to show students how to get an environment variable, or configuration setting for their third-party API keys into JavaScript, I figured I'd quickly find out NET Core. Open your appsettings.json file and add a configuration section. For these types of configuration files, we basically use .json file as a configuration file which contains different messages which will be displayed in the application. Now, before starting the program, we will first open the Visual Studio 2017 and create a new Project with Project Type called ASP.NET Core Web Application (.Net Core) and type

Injecting Asp.Net Core configuration into Typescript applications , Injecting Asp.Net Core configuration into Typescript applications One of the weaknesses of a Typescript application, which just is a Javascript applications, is how to Also, with a static JSON file ever we found some issues, like the us to read the configuration file “appSettings.json” from the view itself. The appsettings.json files “Copy to Output Directory” property should also be set to “Copy if newer” so that the application is able to access it when published. The settings are injected in the main method rather than in the startup method as with web apps but the code is essentially the same. static void Main(string[] args) { var

Accessing Configuration Settings in ASP.NET Core -- Visual Studio , NET Core, your web.config file with its <appsettings> section is gone. While appsettings.json holds your settings, to retrieve those settings you'll use ASP. In addition, my CutOffDate setting (when I get around to retrieving it) needs to Please enable JavaScript to view the comments powered by Disqus. We'll start by just using the standard appsettings.json files, and the environment-specific appsettings.json files, just as you would in a default ASP.NET Core app. I've included the environment variables in there as well for good measure, but it's the JSON files we're interested in for this post.

Sharing appsettings.json configuration files between projects in ASP , February 06, 2018 in ASP.NET Core 2.0 Configuration ASP.NET Core ~ 5 min read. Sharing appsettings.json configuration files between projects  Learn six ways to read all settings on the AppSettings.JSON file in an ASP.NET CORE 3.0 web application. In this article, you will learn six ways to read all settings on the AppSettings.JSON file in an ASP.NET CORE 3.0 web application. With the ASP.NET CORE, the WEB.CONFIG file was totally out of use, but if you want to Host an ASP.NET CORE Web

Comments
  • If your javascript inside view and you are using some kind of view engine like razor. You can check that post for solution stackoverflow.com/questions/48979294/….
  • @maximelian1986 The problem is that I just only reffer this Js file in my view like that <script src="~/lib/signalr/signalr.js"></script> <script src="~/js/signalrCustom.js"></script>
  • Work like a charm. Thanks a lot.
  • Note that this implementation is really dangerous as it allows anyone to read any configuration value, including secrets like a connection string or other internal values. If you want to have an endpoint to return configuration values you should either restrict this to very few explicit values and/or have a very strong input validation to prevent people from accessing critical information.
  • This hits controller every time you read the setting value. Try to pull all config values as a list. Make sure your secret is managed.
  • @Kurkula, no this implementation only runs once. It makes the AJAX call to the controller, then executes a function when the AJAX call is complete that would, presumably, do something with the value of parameterValue. However, I do agree with you that the controller would be better implemented to pull all needed configuration values as a list, so that the controller is only hit once for each page view. In its current form, the AJAX call is not reusable when you need multiple configuration values, but that was not part of the OP's question.
  • I believe this is a better solution .
  • I use this method in several of my web applications. However, I recently hit a snag with using Content Security Policy (CSP) to effectively whitelist the scripts that can be executed by the browser. With a CSP script policy in place, I would need to add 'unsafe-inline' as an exception, effectively allowing any inline script to be executed, including something that may be injected maliciously. (Hence why the policy is named "unsafe"). Just something to be wary of if you start adopting CSP!
  • @BlairAllen That does apply to any kind of inline script though. Alternatively, you could also have a controller action that returns a dynamically rendered JavaScript file for example.
  • I would recommend that your controller deny (return null) all parameter names except for the ones that you whitelist. With your current implementation, if you add secret2 to your config but forget to update this controller code, you will expose that new secret value accidentally.