Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape ;(semicolon) in aspnet core connection string in appsettings.json file

I have created a web application. I am trying to connect to MS Sql database through entity framework.

I have an issue in the below connection string in appsettings.json file.

In the connection string password contains semicolon as below.

DefaultConnection": "Server=server name; Database=my db; User Id=myuser; Password=HjkT;tk-k@=?55\; MultipleActiveResultSets=true;

while i am trying to access the entity framework getting this error

An unhandled exception occurred while processing the request.

ArgumentException: Keyword not supported: 'tk-k@'

I tried putting this password in double quotes/single quote/" . But still facing this error.

like image 933
jkyadav Avatar asked Apr 25 '17 04:04

jkyadav


People also ask

How can I get connection string from Appsettings json in net core 5?

json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button. Once the File is created, it will have a DefaultConnection, below that a new Connection String entry is added.

How do I get Connectionstring in .NET core?

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 should go in Appsettings json?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.


1 Answers

That's not appsettings.json issue, it's connection string issue.

The basic format of a connection string includes a series of keyword/value pairs separated by semicolons. The equal sign (=) connects each keyword and its value. To include values that contain a semicolon, single-quote character, or double-quote character, the value must be enclosed in double quotation marks
from here

So your connection string shoud look like:

Server=server name; Database=my db; User Id=myuser; Password="HjkT;tk-k@=?55\"; MultipleActiveResultSets=true;

And as soon as quote and backslash are special symbols for json - you should escape them with backslash. Here is your file:

{
  "DefaultConnection": "Server=server name; Database=my db; User Id=myuser; Password=\"HjkT;tk-k@=?55\\\"; MultipleActiveResultSets=true;"
}
like image 120
Dmitry Avatar answered Nov 06 '22 14:11

Dmitry