I want to create the database connection in my .net core Web API how to I write the connection string. In App setting file how to I write the connection string.
You need to use the connectionStrings tag to specify the connection string in the web.config file as in the following image. Write this XML markup in the web config file for declaration of the connection. You can add many tags for each database connection. Now I will describe how to read a web.config connection string in an ASP.NET web application.
Specify it in the web.config file. Create a common class file for the connection string. The benefit of writing a connection string in a web config file is there is no need to write code every time to connect to the database. It reduces the redundancy of code over a single application.
Open the application root Web.config file shown below. (Not the Web.config file in the Views folder.) Add the following connection string to the <connectionStrings> element in the Web.config file. The following example shows a portion of the Web.config file with the new connection string added: The two connection strings are very similar.
From the File menu, select New > Project. Select the ASP.NET Core Web API template and click Next. Name the project TodoApi and click Create. In the Create a new ASP.NET Core Web Application dialog, confirm that .NET Core and ASP.NET Core 5.0 are selected.
I got my answer
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet5-ConfigurationSample-ad90971f-6620-4bc1-ad28-650c59478cc1;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
here is link http://aspnetmonsters.com/2016/01/json-config-in-aspnetcoremvc/
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