c# - Trying to get key and value from appSettings -
i'm trying information appsettings in project, i'm using if ever require edit information can change app settings page, want extract key , value strings , display page. heres code. in appsettings have,
<?xml version="1.0"?> <appsettings> <add key ="general" value="somevalue1"/> <add key ="other" value="othervalue"/> </appsettings>
in controller page have
namespace mypro.web.controllers { using system.collections.generic; using system.configuration; using system.web.mvc; using models.mypro; public class mycontroller { public actionresult mypro() { dictionary<string, dynamic> myprodata = new dictionary<string, dynamic>(); var general = configurationmanager.appsettings["general"]; var other = configurationmanager.appsettings["other"]; myprodata.add("general", general); myprdata.add("other", other); var mypromodel = new mypromodel(myprodata); return view(mypromodel); } } }
in model have,
namespace mypro.web.models.mypro { using system; using system.collections.generic; using system.configuration; using system.linq; using system.net; using system.net.http; using system.web.http; using mypro.model; public class mypromodel { public mypromodel(dictionary<string, dynamic> myprodata) { this.myprodata = myprodata; } public dictionary<string, dynamic> myprodata { get; private set; } } }
and in cshtml page have
@model mypro.web.models.mypro.mypromodel <div class="content"> <h1 class="sectionheading">contact us</h1> @foreach (var item in model.myprodata) { <div class="contentsection"> <h2>tables</h2> <div class="innercontentsection"> <table class="basetable"> <thead><tr><th>column1</th><th>colum2</th></tr></thead> <tbody> <tr><td>@model.myprodata</td><td>@model.myprodata["general"].</td></tr> <tr><td>@model.myprodata</td><td>@model.myprodata["other"]</td></tr> </tbody> </table> </div> </div> } </div>
my question how column1 show strings general , other , column2 display somevalue1 , othervalue values appsettings, practice i'm trying accomplish or there better way. have tried other methods stack using lists, hashtables etc. there simple way grab or extract data key , value.
you want use allkeys attribute of appsettings. example:
public actionresult mypro() { dictionary<string, dynamic> myprodata = new dictionary<string, dynamic>(); configurationmanager.appsettings.allkeys.tolist().foreach ( settingskey => myprodata.add(settingskey, configurationmanager.appsettings[settingskey]); ); var mypromodel = new mypromodel(myprodata); return view(mypromodel); }
as mentioned below, if want subset of keys output...
first add new item config:
<add key ="configitemstomonitor" value="general,other"/>
then use key generate list output:
public actionresult mypro() { dictionary<string, dynamic> myprodata = new dictionary<string, dynamic>(); configurationmanager.appsettings["configitemstomonitor"].split(',').tolist().foreach ( settingskey => myprodata.add(settingskey, configurationmanager.appsettings[settingskey]); ); var mypromodel = new mypromodel(myprodata); return view(mypromodel); }
you decide monitor value of configitemstomonitor - add comma separated list of values. make sure exclude spaces.
Comments
Post a Comment