Asp.net

在 web.config 中創建一個字元串並在 web.api 中使用它

  • April 25, 2015

我是 Web 開發領域的新手,我想在 web.config 文件中創建一個變數,以便可以在 web.api 的 .NET 部分中使用它

我找到了以下關於如何做到這一點的教程:

在 ASP.NET 中設置連接字元串到 SQL SERVER

http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config

我有以下問題,我沒有將字元串連接到的數據庫(我只會在網路配置中使用它,這樣我就可以輕鬆更改字元串而無需通過程式碼。所以假設我正在使用它以下方式:

<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />

我應該在connectionString和 中有providerName什麼?

如果我了解您想要做什麼,聽起來您根本不想使用連接字元串。相反,請使用 web.config 文件的應用程序設置部分。例如

<configuration>
 <system.web> ... </system.web>
 <appSettings>
   <add key="MyAppSetting" value="A test value." />
 </appSettings>
</configuration>

然後可以通過獲取的值在您的程式碼中使用它

System.Configuration.ConfigurationManager.AppSettings["MyAppSetting"]

(C#) 或

System.Configuration.ConfigurationManager.AppSettings("MyAppSetting")

(五)

有關詳細資訊,請參閱MSDN,或僅線上搜尋“asp.net AppSettings”。

引用自:https://stackoverflow.com/questions/15880443