asp.net - 在 web.config 中创建一个字符串并在 web.api 中使用它

标签 asp.net web-config connection-string asp.net-web-api

我是 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/

相关文章:

css - 透明元素不允许我点击东西

asp.net-mvc - 基于Active Directory角色的权限

c# - 未指定计算机名称的 SQL Server 连接字符串?怎么可能?

asp.net - 虚拟主机上的 MVC 3 ASPNETDB.MDF SqlExpress 数据库连接字符串

python - Blob存储: connection to emulator,本地开发失败

c# - 插入后更新 GridView

asp.net - 在global.asax文件的页面中返回错误时的方法名称和行号

c# - 从同一解决方案 C# 中的另一个项目引用 Web.Config 文件

asp.net - 以编程方式编辑 Web.config

asp.net - 如何发送post请求?