2017-06-07 55 views
1

我收到以下錯誤,當我運行代碼:誤差的web.config和CDATA

System.Configuration.ConfigurationErrorsException:「配置節不能包含CDATA或文本元素」

應用程序構建得很好,但是當我選擇鏈接轉到應用程序中的「關於」視圖時,它會引發上述錯誤。

這是它突出的代碼行:

string conn = ConfigurationManager.ConnectionStrings["CountryConnectionString"].ConnectionString; 

,這是在web.config文件中的連接字符串:

<add name="CountryConnectionString" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=TestData" providerName="System.Data.SqlClient" />  

如果更多的是讓我知道和需要我可以發表任何你需要的東西

這裏是整個網頁配置文件

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please  visit 
    https://go.microsoft.com/fwlink/?LinkId=301880 
    --> 
<configuration> 
    <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" /> 
    <httpRuntime targetFramework="4.5" /> 
    <httpModules> 
      <add name="ApplicationInsightsWebTracking"  type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
</httpModules> 
    </system.web> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral"  publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
     </dependentAssembly> 
    <dependentAssembly> 
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
    </dependentAssembly> 
    <dependentAssembly> 
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
    </dependentAssembly> 
    <dependentAssembly> 
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
    <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
    </dependentAssembly> 
</assemblyBinding> 
    </runtime> 
    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules> 
     <remove name="ApplicationInsightsWebTracking" /> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 
</modules> 
    </system.webServer> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
</compilers> 
    </system.codedom> 
    <connectionStrings> 
<add name="CountryConnectionString" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=TestData" providerName="System.Data.SqlClient" />  
    </connectionStrings> 
</configuration> 
+0

有可能在你的配置文件中的另一個錯誤。如果是這樣,它會在第一次訪問時產生錯誤。順便說一句:CountryConnectionSrting包含一個錯字。 – realbart

+0

好的,我修復了錯字,它仍然會拋出錯誤(我覺得很愚蠢的發佈錯字開始) – Max

+0

你能發佈更多的配置文件嗎?至少完整的connectionstrings部分? – realbart

回答

0

簡短解決方案:刪除標籤之間的空格。

如果我複製你的配置包含一個呈現爲空白但實際上不是的字符。除了空格和製表符之外,配置解析器都是文本。文本在配置文件中是不允許的。

// the spacing character 
Encoding.UTF8.GetBytes(new[] { ' ' })[0] // yields 194 
Encoding.UTF8.GetBytes(new[] { ' ' })[1] // yields 160 

// the actual space 
Encoding.UTF8.GetBytes(new[] { ' ' })[0] // yields 32 

嘗試將配置更改爲:

<connectionStrings><add name="CountryConnectionString" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=TestData" providerName="System.Data.SqlClient" /></connectionStrings> 

重新添加空格之前和進入

<connectionStrings> 
    <add name="CountryConnectionString" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=TestData" providerName="System.Data.SqlClient" /> 
</connectionStrings> 
+0

這是完美的。現在要弄清楚爲什麼在那裏開始:-) – Max