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?
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.
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.
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.
Connection strings can be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file.
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;
}
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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With