0

I have an api endpoint that returns these settings to a client HTML/JS that retrieve these data through either normal XML request or promise or async/await.

return new JsonResult(
                new List<object>()
                {
                    new { id = 1, size = "big", order = 6},
                    new { id = 2, size = "small", order = 4},
                    new { id = 3, size = "medium", order = 2},
                    new { id = 4, size = "small", order = 5},
                    new { id = 5, size = "small", order = 8, chips= new { }},
                    new { id = 6, size = "small", order = 7},
                    new { id = 7, size = "big", order = 1, chips= new { }},
                    new { id = 8, size = "small", order = 3},
                    new { id = 10, size = "big", order = 9},
                    new { id = 20, size = "big", order = 10}
                });

however a new requirement come up that needs me to save these settings into a class and their value into a config file so we do not need to rebuild every time new settings are added.
This is the class I made:

 public class Settings
    {
        public int Id { get;}
        public string Size { get;}
        public int Order { get;}
        public int[] Arrays { get;}
    }

I have been looking at creating configuration file and calling them. I just cannot wrap my head around how to save these value into a configuration file in an api so that it can be call in a client like this:

const getDataByPromise = function (method, url) {
  return new Promise(function (resolve, reject) {
    const xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.onload = function () {
      if (successfulHttpResponse(this.status)) {
        resolve(xhr.response);
        document.getElementById("promise").textContent = xhr.responseText;
      } else {
        reject({
          status: this.status,
          statusText: xhr.statusText,
        });
      }
    };
    xhr.onerror = function () {
      reject({
        status: this.status,
        statusText: xhr.statusText,
      });
    };
    xhr.send();
  });
};

How do you think I should go about implementing this

Noelia
  • 39
  • 7
  • " looking at XML file and .init config file there are not that many source on them I guess" sorry mate, but this is difficult to believe. Actually if you use json then have a look at [How to write a JSON file in C#?](https://stackoverflow.com/questions/16921652/how-to-write-a-json-file-in-c) – Mong Zhu Jan 14 '21 at 07:44
  • @MongZhu hey there thank you for answering. I think you might misunderstood my point. I already has a list of settings with properties as mentioned above and I don't really need to write them anywhere. My problem is should I have an app.config file or something like just a normal json/xml file is sufficient. – Noelia Jan 14 '21 at 07:51
  • I understood: you have values that work as settings. These values need to be manipulated independent of your program and then loaded into the program. Is that correct? – Mong Zhu Jan 14 '21 at 08:40
  • @MongZhu exactly haha. I read this article https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/ and it said that "Developers can put settings in configuration files, eliminating the need to recompile an application every time a setting changes." which seems to satisfy the requirement. I just cannot wrap my head around how to create a configuration file that can be return by a controller with value according to a class. – Noelia Jan 14 '21 at 08:50
  • OK we are getting closer. have a look at [this post](https://stackoverflow.com/questions/453161/how-can-i-save-application-settings-in-a-windows-forms-application) will it help? – Mong Zhu Jan 14 '21 at 08:53

0 Answers0