2

I'm writing an MVC 4 application using with AngularJS and Razor views. I want to pass the Web.config values to my AngularJS controller. How can I do that? Any idea?

tronman
  • 9,862
  • 10
  • 46
  • 61
Ajay Srikanth
  • 1,095
  • 4
  • 22
  • 43

2 Answers2

4

If you have a razor template that is rendering your angular script elements, you can add to it an inline startup script like this:

Whatever.cshtml:

<script type="text/javascript">
        angular.module('app')
            .provider('appConfig', function appConfig () {
            var values = {
                SomeConfigValue: '@ConfigurationManager.AppSettings["Setting1"]'
            };
            return {
                $get: function () {
                    return values;
                }
            };
        });
</script>

And then inject 'appConfig' wherever you need it in your angular controllers/services/etc.

jlew
  • 10,491
  • 1
  • 35
  • 58
  • hi jlew, thanks for the response. I've tried this and getting an error near the module name saying "is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you..I'm using the same module name that my app has and also tried a different name. In the controller I've just passes appConfig as a parameter I've tried both with '$' and without '$', what is the correct way to pass...sorry I'm new to angularJS. How does my module know about this 'app' if i don't add it as dependency. – Ajay Srikanth Jan 07 '15 at 19:46
  • It should be your application's module name, and you have to ensure that the script that actually creates your module is run before this (provider) script runs. – jlew Jan 07 '15 at 19:56
  • client not able to access ConfigurationManager – ayush narula Nov 20 '17 at 22:33
1

They are by nature inaccessible to client-side libraries (i.e. Javascript), if you wanted to write a mvc controller that exposed your web.config settings, that would be about your best option.

Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62