1

I am attempting to use IConfigurations to create a connectionstring:

public class ShippingRequestController: Controller
    {
        private readonly IConfiguration Configuration;

        public ShippingRequestController(IConfiguration _configuration)
        {
            Configuration = _configuration;
        }
    }

And get the connection string by

Configuration.GetConnectionString("ShipRequestDBString")

This works well, however I need to create an instance of this controller in a separate controller and I don't know what to use for the IConfiguration parameter or if that is even possible.

ShippingRequestController mySRControl = new ShippingRequestController(WHAT DO I PUT HERE);
Matt R
  • 37
  • 5
  • "create an instance of this controller in a separate controller " Do you want to Call a controller from another controller ? "I don't know what to use for the IConfiguration parameter or if that is even possible." In another controller you can inject IConfiguration like your ShippingRequestController. What do you mean ? Could you explain more ? – Qing Guo Jul 13 '23 at 05:24
  • I would like this controller to be able to read the connectionstring data from the appsettings.json file but I am not sure what to put in the parameter when it is called by a separate controller – Matt R Jul 13 '23 at 12:36

1 Answers1

0

Found this answer at Is ConfigurationManager.AppSettings available in .NET Core 2.0? from Yusif Karimov

namespace DemoWeppApp
{
    public static class StaticConfigurationManager
    {
        public static IConfiguration AppSetting { get; }
        static StaticConfigurationManager()
        {
            AppSetting = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json")
                    .Build();
        }
    }
}

This removes the requirement of a parameter in the constructor.

Matt R
  • 37
  • 5