c# - 在 web.config 中放置连接字符串的位置

标签 c# asp.net-mvc connection-string

我的 web.config 看起来像这样:

<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="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
  </system.web>
  <runtime>

当我在 <configuration> 下方添加连接字符串时我收到一条错误消息,说只有一个 <configSections>元素是允许的。我应该将连接字符串放在哪里?

最佳答案

只要把它放在<configuration>里面就行了在</configSections>之后f.e.

<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>
    <connectionStrings>
        <add name="DefaultConnection" connectionString="blablabla" providerName="System.Data.SqlClient" />
    </connectionStrings>    
    <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    <system.web>
        <compilation debug="true" targetFramework="4.5.1" />
        <httpRuntime targetFramework="4.5.1" />
    </system.web>
    ...

关于c# - 在 web.config 中放置连接字符串的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26882704/

相关文章:

c# - NodeJS 和 C# 中的 AES256 加密/解密

javascript - 单击下拉列表 MVC 4 时显示/隐藏

asp.net-mvc - ASP.Net Html.DropDownList 未选择所选元素

asp.net-mvc - ASP.NET MVC4 在错误区域搜索 Controller

asp.net - 从非域 Web 主机连接到 SQL Server 的最佳方法

asp.net-mvc - 与 SQL Server 建立连接时发生与网络相关或特定于实例的错误

c# - 连接到受密码保护的 Web 目录

c# - 写入包含 ""的字符串

asp-classic - 在不同的端口号上访问 SQL(经典 ASP 连接字符串)

c# - 在 C# 中的 Repeaters 中更改数据绑定(bind)控件的值