Reputation: 27360
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 = ;
Upvotes: 7
Views: 2367
Reputation: 4173
If it is not too fancy , you can have a hidden variable and then access it in javascript.
Upvotes: 0
Reputation: 1039110
var checkTimer = @Html.Raw(Json.Encode(ConfigurationManager.AppSettings["CheckTimer"]));
Upvotes: 7
Reputation: 10940
Check out the Javascript serializer:
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
Upvotes: 0