jaffa
jaffa

Reputation: 27360

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>        
    <!-- 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

Answers (3)

Aravind
Aravind

Reputation: 4173

If it is not too fancy , you can have a hidden variable and then access it in javascript.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039110

var checkTimer = @Html.Raw(Json.Encode(ConfigurationManager.AppSettings["CheckTimer"]));

Upvotes: 7

Related Questions