I have hard time to find out solution for an issue regarding binding values from configuration files.
I have configuration option containing list initialized in code, for example
public List<string> availableFruit { get; set; } = new List<string> { "apple", "banana", "pineapple",};
But I want to change it in config file where I have
{
"Store": {
"AvailableFruit": [
"watermelon"
]
}
}
The binding now works that way, that it adds config in file to the previously initialized in code, thus it results in "apple, banana, pineapple, watermelon", but I want to have only watermelon.
I digged through code and found out, that .net has it hardcoded in configuration binder that it uses "Add" method. I also tried to create new method where I wanted override the method, but that was not possible.
Is there any way, how can I do that?
//EDIT: I call it as this
services.Configure<StoreOptions>(Configuration.GetSection("Store"));
public class JsonHelper
{
/**
* JsonHelper.JsonFileAddParameter("~/app.json", "hot", "YEEE");
* */
public static void JsonFileAddParameter(string path, string parameter, string value)
{
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("Value");
if (String.IsNullOrEmpty(parameter))
throw new ArgumentNullException("Parameter");
dynamic treatments;
string _path = HostingEnvironment.MapPath(path);
using (StreamReader sr = new StreamReader(_path))
{
treatments = JsonConvert.DeserializeObject(sr.ReadToEnd());
sr.Close();
}
//JObject property = new JObject( new JProperty(parameter, value) );
treatments[parameter] = value;
File.WriteAllText(_path, String.Empty);
string json = JsonConvert.SerializeObject(treatments);
File.WriteAllText(_path, json);
}
/**
* JsonHelper.JsonFileEdit("~/app.json", "AppKey", "123456789");
* */
public static void JsonFileEdit(string path, string parameter, string editValue)
{
if (String.IsNullOrEmpty(editValue))
throw new ArgumentNullException("Edit Value");
if (String.IsNullOrEmpty(parameter))
throw new ArgumentNullException("Parameter");
dynamic treatments;
string _path = HostingEnvironment.MapPath(path);
using (StreamReader sr = new StreamReader(_path))
{
treatments = JsonConvert.DeserializeObject(sr.ReadToEnd());
sr.Close();
}
if (treatments[parameter] == null)
throw new ArgumentNullException(parameter);
treatments[parameter] = editValue;
File.WriteAllText(_path, String.Empty);
string json = JsonConvert.SerializeObject(treatments);
File.WriteAllText(_path, json);
}
/**
* var lol = JsonHelper.JsonReader("~/app.json", "AppKey");
* */
public static dynamic JsonReader(string path, string parameter)
{
dynamic treatments;
using (StreamReader sr = new StreamReader(HostingEnvironment.MapPath(path)))
{
treatments = JsonConvert.DeserializeObject(sr.ReadToEnd());
sr.Close();
}
return treatments[parameter];
}
/**
* var lol = JsonHelper.Json("~/package.json");
* */
public static dynamic JsonReader(string path)
{
/**
* using(StreamReader sr = new StreamReader(Server.MapPath("~/Content/treatments.json")))
{
treatments = JsonConvert.DeserializeObject<List<Treatment>>(sr.ReadToEnd());
}
* */
dynamic treatments;
using (StreamReader sr = new StreamReader(HostingEnvironment.MapPath(path)))
{
treatments = JsonConvert.DeserializeObject(sr.ReadToEnd());
sr.Close();
}
return treatments;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.