asp.net - Passing AppSettings to external javascript file the MVC way? -


i have settings in appsettings (web.config) , need pass them external javascript file.

in asp.net think of ashx handler write javascript file response replacing placeholders settings values.

is there better way in asp.net mvc? thank you.

you send them via jsonresult?

in js, you'd have request sends get/post request particular action (let's call getappsetting(), , corresponding value returned in response.

for security reasons, restrict can requested though...

public jsonresult getappsetting(string id) {   //you check what's been requested here if want make sure you're returning information may not wish send.   string appsetting = appsettings[id];    if(string.isnullorempty(appsetting) == false)   {     return json(appsetting, jsonrequestbehavior.allowget);   }    //handle non-existent settings here...   throw new exception("this setting not exist"); } 

alternatively, has been suggested chris marisic in comments may want absolutely limit specific set of key/values developer reasons. therefore, here quick example of that...

public jsonresult getappsettings() {   var appsettings = new dictionary<string, string>();   appsettings.add("myappsetting1", appsettings["myappsetting1"]);   appsettings.add("myappsetting2", appsettings["myappsetting2"]);   appsettings.add("myappsetting3", appsettings["myappsetting3"]);    return json(appsettings, jsonrequestbehavior.allowget); } 

note jsonrequestbehavior.allowget in jsonresults (mvc 2 only). because, default, asp.net mvc 2 not allow get requests on actions return jsonresult. can circumvent adding jsonrequestbehaviour, should mention should consider doing post request in order retrieve information, , remove behaviour in action.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -