Can't resolve |DataDirectory| in web.config connection string RRS feed

  • Question

  • User409414831 posted

    I want to use the |DataDirectory| substituion string for the path of my database file, because I dont't want to change the path after deployment. My Project is an ASP.NET Web API with .NET Framework 4.7.2. I am using Visual Studio 2019.

    The following connection string for an sqlite database is stored in my web.config:

    <add name="sampleDB" connectionString="Data Source=|DataDirectory|\sample.db;Version=3;" providerName="System.Data.Sqlite"/>

    The database file is located in App_Data/sample.db. When I try to retrieve the ConnectionString:


    The value is "Data Source=|DataDirectory|\\sample.db;Version=3;" like the value in the web.config. I thought it will be resolved to the corresponding path of my App_Data folder. When I log this:


    I receive the correct path of the App_Data folder. Does any one know why |DataDirectory| will not be resolved to the correct path?

    Monday, July 20, 2020 9:21 AM


All replies

  • User753101303 posted


    This placeholder is not processed by ConfigurationManager.ConnectionStrings.

    I don't remember but try to use this connection string and see what happens. If it still doesn't work with SqliteConnection then https://docs.microsoft.com/en-us/dotnet/api/microsoft.data.sqlite.sqliteconnectionstringbuilder?view=msdata-sqlite-3.1.0 will surely process the placeholder for you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 20, 2020 9:39 AM
  • User409414831 posted


    This placeholder is not processed by ConfigurationManager.ConnectionStrings.
    I don't remember but try to use this connection string and see what happens. If it still doesn't work with SqliteConnection then
    https://docs.microsoft.com/en-us/dotnet/api/microsoft.data.sqlite.sqliteconnectionstringbuilder?view=msdata-sqlite-3.1.0 will surely process the placeholder for you.

    SQLiteConnection will throw an error:  "unable to open database" with the connection string from the web.config.
    The SqliteConnectionStringBuilder will work for sure. I am doing something simmilar to receive the path now.

    First I use the value of DataDirectory in AppDomain:

    private static string GetDatabasePath(string databaseFullName)
    var appDataPath = AppDomain.CurrentDomain.GetData("DataDirectory");
    return $"{appDataPath}\\{databaseFullName}";

    and then return this:

    public static string ConnectionString(string name)
        switch (name)
                case "sample":
                    return $"Data Source={GetDatabasePath("sample.db")};Version=3;";
                    throw new System.ArgumentException("database not found");

    Thank you for your help.

    Monday, July 20, 2020 9:54 AM
  • User753101303 posted

    If you want compare with new SqliteConnectionBuilder(connectionString).ToString()

    The input connection string should contain the placeholder and the output string should have been processed (I can't try right now).

    Monday, July 20, 2020 11:09 AM