0

In ASP.NET Core with Razor Pages solution, I have an appsettings.development.json file like below.

I want to get the value of APP_ID from the appsettings.development.json file in _Layout.cshtml file. How to do this?

I have tried like below, it shows console error in browser - "Uncaught SyntaxError: missing ) after argument list". Please let me know how to use this value from the json file:

@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<!DOCTYPE html>
<html lang="en">
<head>
    <script>
        console.log('from settings ' + @Configuration["APP_ID"]);

…

My appsettings.development.json file:

{
  "APP_ID" : "xxxxxxxxxx",
  "APP_Value" : "zzzzzzzzzzzzz"
}
2
  • The reason you're getting "Uncaught SyntaxError: missing ) after argument list" error is that you need to escape the @Configuration["APP_ID"] value for it to be parsed as valid javascript. Use @Html.Raw(Json.Encode(val)) Commented Jul 12, 2021 at 10:52
  • Thank you, One more question I have same "APP_ID" value in both appsettings.json and in launchsettings.json. However using Microsoft.Extensions.Configuration @inject IConfiguration Configuration --- fetches value from launchsettings.json only and not from appsettings.json. Any idea how to fetch the value of "APP_ID" from appsettings.json file ? Commented Jul 12, 2021 at 11:43

1 Answer 1

1
console.log('from settings ' + @Configuration["APP_ID"]);

You have to keep in mind that rendering something on the server using Razor has nothing to do with how that is being interpreted in the browser.

When Razor renders the above, it will write the following to the result (when APP_ID = "xxxxxxxxxx"):

console.log('from settings ' + xxxxxxxxxx);

As you can see, there is now the value of your APP_ID written directly inside the console.log call. Razor does not know what it is writing though so this is how it will produce the output.

Then, when this is being received and executed by the browser, the browser will see that + xxxxxxxxxx part which, depending on the actual value, will fail with one of many possible errors. Because from the JavaScript view point, this is invalid.

So in order to fix this, you will have to make sure that your Razor view produces valid code for the browser. This applies to both JavaScript and HTML too. One way to do this is to use the JSON serializer that is available within Razor views. Since JSON is the JavaScript notation, it will always be valid JavaScript, so you can use this well for passing data from Razor to JavaScript.

In your case, you could write it like this:

@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<!DOCTYPE html>
<html lang="en">
<head>
    <script>
        console.log('from settings ' + @Json.Serialize(Configuration["APP_ID"]));

…

Note the Json.Serialize call around your value. When the JSON serializer receives just a string, it will properly enquote it – and also take care of properly escaping any quote within the value itself. So this is a very safe way to embed your value into JavaScript.

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

5 Comments

Thank you for the reply, as u mentioned it works fine with @Json.Serialize(Configuration["APP_ID"])
One more question I have same "APP_ID" value in both appsettings.json and in launchsettings.json. However using Microsoft.Extensions.Configuration @inject IConfiguration Configuration --- fetches value from launchsettings.json only and not from appsettings.json. Any idea how to fetch the value of "APP_ID" from appsettings.json file ?
The launchsettings shouldn’t be used, unless you mean that you configured an environment variable there. In that case, you cannot explicitly retrieve the value from the appsettings.json (unless loading the file explicitly again) since the configuration is supposed to be able to overwrite each other (with appsettings.<environment>.json overwriting the appsettings.json, and the environment variables being able to overwrite both). – The launchsettings file only exists during development anyway, so you can just remove the configuration there and use the appsettings files only.
Can I update Configuration["APP_ID"] using javascript?
@desmondng Not out of the box. You could have some custom configuration service though, which value defaults to the IConfiguration value and which then offers some API endpoint to change the value.

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.