How to set Connection String Globally in ASP .NET Core

In this article we discuss how to set Connection String Globally in ASP .NET Core. Following this article it should clear how to Find Connection String in ASP .NET Core. To find more content related to ASP.NET Core please follow the link.

Generally, in ASP .NET Core application we fetch the connection string from appsettings.json file using Dependency Injection. But using this article we will see how to set the connection string globally and use anywhere in the application without declaring each time.

Let’s take ASP .NET Core MVC application and see how to set connection string dynamically in ASP.NEt Core and access it anywhere in the application.

Let’s start doing the below things to achieve this example.

  • Creating ASP.NET Core Application
  • Adding the Connection String in Appsettings.json file.
  • How to set the connection string globally and access in entire application.
Creating ASP.NET Core Application

We are going to develop ASP.NET Core Web Application for that we are going to choose “ASP.NET Core Web Application” template.

Core Spider - ASP.NET Core Project Template
CoreSpider – ASP.NET Core Project Template

On the next step set the project name like “SetGlobalConnection” (or you can set any name as per your convenient) and then choose location of project. Select ASP .NET Core Version 3.1 as a framework for application and remember few advance settings for such as configuring https and docker we are not enable for now so unchecked the settings for this project and then click the Create button.

Core Spider - MVC Template
CoreSpider-ASP.NET Core MVC Template

The project structure of the ASP.NET Core MVC application is look like below;

Core Spider - ASP.NET Core Project Strcture
CoreSpider- ASP.NET Core MVC Project Structure
Adding the Connection String in Appsettings.json file.

We need to add the connection string inside the appsettings,json file as like below;

How to set the connection string Globally and access in entire application

To declare the connection string globally we need to customize the startup.cs class like below;

Core Spider- appsettings.json file in startup.cs

Code Snippet Explanation

IConfiguration has two specializations,

  • IConfigurationRoot is used for root node. It can trigger a reload. IConfigurationSection Represents a section of configuration values. The GetSection and GetChildrenmethods return an IConfigurationSection. Use IConfigurationRoot when reloading configuration or for access to each provider.
  • Here appsettings.json file is dynamically loaded when the startup class is called.

To Read the connection string value  inside configure() method of startup.cs class we need to access the static variable like below;

ConnectionString = configRoot.GetConnectionString("DefaultConnection");
Core Spider- Connection String in Configure() method
Core Spider- Connection String in Configure() method

Now we have set the connection string dynamically in startup.cs class. Let’s go to the Controller page and access it inside the HomeController.cs File like below;

  public IActionResult Index()
        {
            string myconn = Startup.ConnectionString;
            return View();
        }
Core Spider- Fetch Connection in HomeController()
Core Spider- Connection String in Configure() method

You can see that, the connection string now accessible in the controller page by just accessing the startup.connectionstring as inside we declare the connectionstring variable as static and append the connection string into it inside configure() method.

Video Tutorial – How to set Connection String Globally in ASP .NET Core
Core Spider – Set Connection String Globally in ASP .NET Core

Conclusion

In this article we explain how to get and set connection string dynamically and access anywhere in the ASP.NET Core MVC application. If have any doubt or concern about this article then please give your feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *