2

I want to access the "BaseURL" key value in my user defined class instead of controller. Please help me how to do the same

Below is My AppSettings.json

{
    "Logging": {
    "IncludeScopes": false,
        "LogLevel": {
        "Default": "Warning"
        }
     },
    "BaseURL": "http://192.168.0.72/mehcrm/CMS/public/api/v1/"`
}

Need your assistance.

1 Answer 1

2

You can take "BaseURL" value in Startup.cs like mentioned in below code.

public class Startup
{

 public static string BaseUrl { get; private set; }

    public Startup(IHostingEnvironment env)
    {
          var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();

        BaseUrl = Configuration.GetSection("BaseURL").Value;
    }
 } 

    public static string GetBaseUrl()
    {
        return Startup.BaseUrl;
    }

Use it in other classes in the project like shown below:

string BaseUrl= Startup.GetBaseUrl();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.