Update appsettings programmatically asp.net




















It isn't clear what you mean by "change and save. Option number 1, Sir. What I managed to do was to move those two settings to another file - installationsettings. Siemko, so how did you update the json setting file? Did you use the Configuration framework or did you open the file to edit it? Manuel: I open the file, modify it and then save. Siemko, thanks that's the only way I found myself. It would be nicer to have in IOptions a method to save the changes. Show 1 more comment.

Active Oldest Votes. Improve this answer. Deilan 4, 3 3 gold badges 32 32 silver badges 49 49 bronze badges. Mathias Mathias 1, 3 3 gold badges 17 17 silver badges 29 29 bronze badges.

Add a comment. Here is a relevant article from Microsoft regarding Configuration setup in. Net Core Apps: Asp. Net Core Configuration The page also has sample code which may also be helpful. SetBasePath env. AddJsonFile "appsettings.

NET Core 1. Dmitry Pavlov Ankit Ankit 2, 3 3 gold badges 17 17 silver badges 33 33 bronze badges. Thank you Ankit for your answer! This one works perfectly, but only in Startup class, I cannot access Configuration object from Controller to use it to change configuration files.

And I need two variables that will act like global static and accessed by build-in dependency injection, but also changeable from controller. Siemko ok, I think you can trying parsing the configuration file yourself and modify the values directly and keep reloadOnChange value to true in startup. Thank you once again, that's something I wanted to avoid, but if there's no other way, I have to try that.

Siemko You can access the Configuration object anywhere by registering it with DI. Could that work? It seems, that I have to find another way of storing those settings. Show 3 more comments. Hope it helps someone out, surprised that this isn't a library feature somewhere. Combine AppContext. BaseDirectory, "appsettings. SerializeObject jsonObj, Newtonsoft. Indented ; File. Alex Horlock Alex Horlock 2 2 silver badges 7 7 bronze badges.

Thanks, Alex: I have a suggested improvement on this code: Add the following line just before the recursive call to prevent a NullRef exception in the case a section does not exist. Update appsettings. NET Core at runtime. Take this sample appsettings. BaseDirectory, "appSettings.

Split ":" [0]; if! Qamar Zaman Qamar Zaman 1, 2 2 gold badges 8 8 silver badges 13 13 bronze badges. Although I liked this approach the code won't function if you have a key without ":". I'd suggest At least for me. Oh, it's because of personal taste then, I actually thought it was logically different somehow. Also, what if for some reason your code fails between the calls? Your application is broken from there on I think. This code erases any comment sibling to MyVariable. Yes, exactly!

This code code be rewritten all variables, but it works. Thank you. The project goals were very simple; get ASP. With a little trial and error, I found the following code would do exactly what I wanted; ConfigurationManager. Sage Sage 4, 1 1 gold badge 20 20 silver badges 26 26 bronze badges. This probably just saved me a few hours.

Thanks so much! ToString ;. Try This: using System; using System. Configuration; using System. Configuration; namespace SampleApplication. Eric Citaire 3, 1 1 gold badge 23 23 silver badges 49 49 bronze badges. Code Alumni Code Alumni 1 1 1 bronze badge. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses.

Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 0. See more linked questions. Related 2. Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings. Remove "URL" ; System. Add "URL","www. Save ; However , it is giving the error that my config file is read-only. Improve this question. Ujjwal Vaish Ujjwal Vaish 1 1 gold badge 7 7 silver badges 21 21 bronze badges.

You should not change config files through run time. If you want to change values save it in DB, cookies, session and etc. Database, flat files, etc. Okay, I'll try that. Please think carefully if you really need this. If you still do - here is a link tha can help you: stackoverflow.

Add a comment. Active Oldest Votes. Will you please try this? Improve this answer. Community Bot 1 1 1 silver badge. Yes it works but gives me a dialog box to continue because "some files have been changed". Anyway I can remove this?



0コメント

  • 1000 / 1000