How to get all the values from appsettings key which starts with specific name and pass this to any array?

asp.net, asp.net-mvc-4, c#, web-config

Solution

I'd use a little LINQ:

string[] repositoryUrls = ConfigurationManager.AppSettings.AllKeys
                             .Where(key => key.StartsWith("Service1URL"))
                             .Select(key => ConfigurationManager.AppSettings[key])
                             .ToArray();

Problem

In my `web.config` file I have ``` <appSettings> <add key="Service1URL1" value="http://managementService.svc/"/> <add key="Service1URL2" value="http://ManagementsettingsService.svc/HostInstances"/> ....lots of keys like above </appSettings> ``` I want to get the value of key that starts with `Service1URL` and pass the value to `string[] repositoryUrls = { ... }` in my C# class. How can I achieve this? I tried something like this but couldn't grab the values: ``` foreach (string key in ConfigurationManager.AppSettings) { if (key.StartsWith("Service1URL")) { string value = ConfigurationManager.AppSettings[key]; } string[] repositoryUrls = { value }; } ``` Either I am doing it the wrong way or missing something here. Any help would really be appreciated.

Original source