我是 Web 开发领域的新手,我想在 web.config 文件中创建一个变量,以便我可以在 web.api 的 .NET 部分中使用它
我找到了以下关于如何做到这一点的教程:
Setting up connection string in ASP.NET to SQL SERVER
和
http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config
我有以下问题,我没有将字符串连接到的数据库(我只会在 Web 配置中使用它,这样我就可以轻松更改字符串而无需通过代码。假设我正在使用它以下方式:
<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")
(VB)
见 MSDN欲了解更多信息,或者只是在线搜索“asp.net AppSettings”。
关于asp.net - 在 web.config 中创建一个字符串并在 web.api 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15880443/