What is the best way to inject configuration settings into Javascript in an MVC app?
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>
&l开发者_运维技巧t;!-- Polling timer to check for alerts -->
<add key="CheckTimer" value="10000"/>
</appSettings>
But in my rendered output I just get the following:
var checkTimer = ;
var checkTimer = @Html.Raw(Json.Encode(ConfigurationManager.AppSettings["CheckTimer"]));
Check out the Javascript serializer:
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
If it is not too fancy , you can have a hidden variable and then access it in javascript.
精彩评论