【问题标题】:Creating a string in a web.config and use it in a web.api在 web.config 中创建一个字符串并在 web.api 中使用它
【发布时间】:2013-04-08 13:31:41
【问题描述】:

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

我有以下问题,我没有将字符串连接到的数据库(我只会在网络配置中使用它,这样我就可以轻松更改字符串而无需通过代码。所以假设我是通过以下方式使用它:

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

connectionStringproviderName 中应该有什么?

【问题讨论】:

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


    【解决方案1】:

    如果我理解您想要做什么,听起来您根本不想使用连接字符串。相反,请使用 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”。

    【讨论】:

      【解决方案2】:

      如果您没有要连接的数据库(这是我从您的问题中了解到的),那么您甚至不需要在 Web.config 中包含 &lt;connectionStrings&gt; 部分。仅当您要连接到数据库时才需要该部分。

      如果您确实使用数据库,则 connectionString 会因身份验证类型、数据库产品(MS SQL Server、MySQL)、驱动程序类型(ODBC、.NET)等多种因素而异。

      “提供者名称”取决于您使用的数据库产品。例如对于 SQL Server 是 "System.Data.SqlClient"

      您可以查看this site 以获得适用于不同身份验证类型、使用的驱动程序等的每个产品的数据库产品和连接字符串的完整列表。

      【讨论】:

        【解决方案3】:

        对于 ASP.NET 4.5 应用程序,我使用 appSettings 进行电子邮件配置。我也在使用连接字符串

        appSettings 需要包含在 connectionStrings 之前而不是 configSections 之前

        <configuration>
          <configSections>
            <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
            <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
          </configSections>
          <appSettings>
            <add key="ContactEmail0" value="service@davincispainting.com" />
            <add key="ContactEmail1" value="estimate@davincispainting.com" />
          </appSettings>
          <connectionStrings>
            <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-DAV3-20150302043828.mdf;Initial Catalog=aspnet-DAV3-20150302043828;Integrated Security=True" providerName="System.Data.SqlClient" />-->
            <!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=*****;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
            <!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=Davincis3;User ID=*****;Password=*****;" providerName="System.Data.SqlClient" />-->
            <!--<add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLSERVEREXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
            <add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLEXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />
          </connectionStrings>
        ...
        

        【讨论】:

        • appSettings needs to be included before connectionStrings not before configSections - 也许对你来说它出于某种原因,但我有一个应用程序在 appSettings 低于 connectionStrings 的情况下运行良好。很确定顺序无关紧要。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        • 1970-01-01
        • 1970-01-01
        • 2013-08-29
        • 2021-11-25
        • 2016-02-23
        相关资源
        最近更新 更多