Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Connection String inside an ASP.NET Core controller

I'm developing an ASP.NET Core 2.0.2 Web API with C# and .NET Framework 4.7.

I want to get the connection string from appsettings.json in a method's controller.

I did it in Startup.cs:

using Microsoft.Extensions.Configuration;

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<MyContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MyContext")));

        [ ... ]
}

But I don't know how to do it in a controller. I have found this tutorial, Configure an ASP.NET Core App, but it uses a class to access configuration's options, public class MyOptions

I have tried to do it like in Startup.cs, Configuration.GetConnectionString("MyContext"), but it doesn't recognize Configuration class.

My question is: How can I get the connection string in a controller?

like image 370
VansFannel Avatar asked Nov 28 '17 08:11

VansFannel


People also ask

Where is connection string in asp net core application?

In ASP.NET Core the configuration system is very flexible, and the connection string could be stored in appsettings. json , an environment variable, the user secret store, or another configuration source. See the Configuration section of the ASP.NET Core documentation for more details.

What is connection string in .NET core?

ASP.NET Core Connection String is mainly used to connect to the database, which can be stored in the appsetting. json. The majority of database providers need some type of connection string to establish a database connection. The connection string contains the perceptive information that requires being safest.

How do I get connection string?

Right-click on your connection and select "Properties". You will get the Properties window for your connection. Find the "Connection String" property and select the "connection string". So now your connection string is in your hands; you can use it anywhere you want.

Where is .NET connection string stored?

Connection strings can be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file.


2 Answers

You may directly inject IConfiguration configuration into your controller (it is registered in DI container by default) :

// using Microsoft.Extensions.Configuration;

public class YourController : Controller
{
      public YourController (IConfiguration configuration)
      {
           var connString = Configuration.GetConnectionString("MyContext");
      }

}

But anyway consider using the IOptions pattern as it will be more flexible.

public class MyOptions
{
    public string ConnString { get; set; }
}

public void ConfigureServices(IServiceCollection services)
{ 
    // Adds services required for using options.
    services.AddOptions();

    services.Configure<MyOptions>(myOptions =>
    {
        myOptions.ConnString = Configuration.GetConnectionString("MyContext");
    });

    ...
}

then

  public YourController ((IOptions<MyOptions> optionsAccessor)
  {
      var connString = optionsAccessor.Value.ConnString;
  }
like image 137
Set Avatar answered Oct 12 '22 00:10

Set


You can inject IConfiguration itself into your controller and get connection string:

public class HomeController : Controller {
    private readonly string _connectionString;
    public HomeController(IConfiguration config) {
        _connectionString = config.GetConnectionString("MyContext");
    }
}
like image 3
Evk Avatar answered Oct 11 '22 23:10

Evk