2016-11-16 4 views
0

、私は以下のセクションを持って、変更された値をweb.configに書き戻す方法は?私のweb.configファイルで

<system.webServer> 
    <rewrite> 
     <rules> 
     <rule name="rule 1D" stopProcessing="true"> 
      <match url="^(.*)$" /> 
      <!--<action type="Rewrite" url="//offline.html" />--> 
     </rule> 
     </rules> 
    </rewrite> 
    </system.webServer> 

は、今私はstopProcessingの値を変更し、バックweb.configファイルにそれを記述する必要があります。これは私がやっている何を間違え

....私はこれまで

using System; 
using System.IO; 
using System.Linq; 
using System.Web.UI; 
using System.Xml.Linq; 

namespace WebApplication3 
{ 
    public partial class About : Page 
    {   

     protected void btn1_Click(object sender, EventArgs e) 
     { 
      var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");    
      var section = configuration.GetSection("system.webServer"); 
      var xdoc = XDocument.Load(new StringReader(section.SectionInformation.GetRawXml())); 
      var dec = xdoc.Descendants("system.webServer"); 
      var stopProcessing = dec.Descendants("rules").SelectMany(i => i.Elements()).Select(s1 => s1.Attribute("stopProcessing")).Single().Value; 
      stopProcessing = "false"; 
      configuration.Save(); 

     } 
    } 
} 

を行っている。しかし変更が反映されていない何ですか?

コンプリートweb.configファイル

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=169433 
    --> 
<configuration> 
    <system.webServer> 
    <rewrite> 
     <rules> 
     <rule name="rule 1D" stopProcessing="true"> 
      <match url="^(.*)$" /> 
      <!--<action type="Rewrite" url="//offline.html" />--> 
     </rule> 
     </rules> 
    </rewrite> 
    </system.webServer> 
    <system.web> 
    <compilation debug="true" targetFramework="4.6.1" /> 
    <httpRuntime targetFramework="4.6.1" /> 
    <pages> 
     <namespaces> 
     <add namespace="System.Web.Optimization" /> 
     </namespaces> 
     <controls> 
     <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" /> 
     </controls> 
    </pages> 
    </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="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.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.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
</configuration> 

答えて

0

重要: web.configファイルを変更するには、一般的にはアプリケーションの再起動が発生します。

アプリケーションに柔軟な設定が必要な場合は、ルートやカスタムHttpHandlersを使用したり、XMLファイル/ sまたはDBからデータを読み書きするなど、別のアプローチを検討する必要があります。ここで

は、設定ファイルでの作業のために便利になるかもしれない何かである:

WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath); 
// "WebConfigurationManager" is located in the "System.Web.Configuration" namespace. 
+0

あなたのアドバイスをOK..following、私は変更を行って、答えを更新しました。しかし、変更された値は保存されません。あなたは助けてもらえますか? –

+0

最初に提供した例のコードを使用している場合は、実際にコンテンツを変更しないという理由があります。 "GetRawXml"メソッドは生セクションデータを返します。保存する前にコードに "section.SectionInformation.SetRawXml(modifiedXmlData)"を追加する必要があります。 –

関連する問題