What is the best way to inject configuration settings into Javascript in an MVC app? I've seen how it is done using ASP.NET webforms, but not sure how to do this with MVC.
@using System.Configuration
...
var checkTimer = @ConfigurationManager.AppSettings["CheckTimer"];
In Web.config:
<appSettings>
<!-- Polling timer to check for alerts -->
<add key="CheckTimer" value="10000"/>
</appSettings>
But in my rendered output I just get the following:
var checkTimer = ;
The recommended approach is to put in a separate JavaScript file or inside a section defined in Layout page. A section can be added in the MVC Layout page using @RenderSection() directive. For example, we can define a section in Layout page under <head> tag for scripts like below.
JavaScript can be used in asp.net mvc. If you go for Asp.NET Web forms, there is no need to have a detailed understanding of HTML, CSS or JavaScript as it abstracts all these details and provides automatic plumbing.
<add key="serverip" value="###. ##. #. ##"/>
ASP.NET MVC configuration In ASP.NET apps, configuration uses the built-in . NET configuration files, web. config in the app folder and machine. config on the server. Most ASP.NET MVC and Web API apps store their settings in the configuration file's appSettings or connectionStrings elements.
var checkTimer = @Html.Raw(Json.Encode(ConfigurationManager.AppSettings["CheckTimer"]));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With