0

I`m starting a small open source project in .Net Core 2.0, to learn about this thechnology. I have a empty project and I need to read in the beginig from a remote URL feed. I need thath, the URL be able to be modified, and I thing thah the best way is writing it in the appsettings.json file of the project. But I don´t know how to read this file from the Startup.cs class.

I have tried this, but does not work in .Net Core projects

ConfigurationManager.AppSettings["DataURL"];

This is the content of the Startup.cs class:

public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World!");
            using (WebClient wc = new WebClient())
            {
                string jsonData = wc.DownloadString("http://servizos.meteogalicia.gal/rss/observacion/ultimos10minPlataformas.action");
                JObject jsonObject = JObject.Parse(jsonData);
            }

        });
    }
}
scores
  • 3
  • 1
  • 1
    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#access-configuration-during-startup – nlawalker Jun 03 '19 at 14:06
  • it might be that `System.Configuration.ConfigurationManager` nuget is need, ref: https://stackoverflow.com/questions/47591910/is-configurationmanager-appsettings-available-in-net-core-2-0 – Renat Jun 03 '19 at 14:07
  • I have done it (add System.Configuration.Configurationmanager in nuget package manager). But ConfigurationManager.AppSettings in empty. My appsettings.json file has this contents: { "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*", "DataURL": "http://servizos.meteogalicia.gal/rss/observacion/ultimos10minPlataformas.action" } – scores Jun 03 '19 at 14:33
  • Thanks @nlawalker, i have solved it with your link. – scores Jun 03 '19 at 14:46

2 Answers2

0

you could deserialize the appsettings.json with Newtonsoft.

1: create an class that represents the json file. 2: read the file 3: convert the stream using newtowsoft into een object of the class at point 1.

source code here.

public class Movie
{
    public string Name { get; set; }
    public int Year { get; set; }
}

// read file into a string and deserialize JSON to a type
Movie movie1 = JsonConvert.DeserializeObject<Movie>(File.ReadAllText(@"c:\movie.json"));

// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"c:\movie.json"))
{
    JsonSerializer serializer = new JsonSerializer();
    Movie movie2 = (Movie)serializer.Deserialize(file, typeof(Movie));
}
0

using dependancy injection, you'll get the IConfiguration object done for you. see this

public class Startup
{
    private readonly IConfiguration _ConfigurationManager ;

    public Startup(IConfiguration config)
    {
        _ConfigurationManager = config;
    }
......

you'll now be able to read appsettings values using var DataURL =_ConfigurationManager ConfigurationManager["DataURL"];

Bellash
  • 7,560
  • 6
  • 53
  • 86