【解決方法】asp.net の実行時に web.config ファイルにコメントを追加する方法

プログラミングQA


親愛なるみんな、

こんにちは。実行時に web.config ファイルにコメントを追加したいと考えています。
たとえば、

元の Web.config ファイルは次のとおりです。

C#
<pre lang="xml"><?xml version="1.0"?>
<!--
    Note: As an alternative to hand editing this file you can use the
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in
    machine.config.comments usually located in
    \Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
    <appSettings>
  <add key="customsetting1" value="Some text here" />
  <add key="myKey" value=",1,1,1,1,1,1" />
  <add key="RemoveTCPError" value="1,1" />
 </appSettings>
    <connectionStrings>
  <add name="myDatabaseName" connectionString="whatever" />
 </connectionStrings>
    <system.web>
        <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->
        <compilation debug="true"/>
        <!--
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
        -->
        <authentication mode="Windows"/>

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>

    </system.web>
</configuration>

いくつかのボタンをクリックすると、次のように変更された後、 :

XML
<?xml version="1.0"?>
<!--
    Note: As an alternative to hand editing this file you can use the
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in
    machine.config.comments usually located in
    \Windows\Microsoft.Net\Framework\v2.x\Config
-->

<!--
    Note: As an alternative to hand editing this file you can use the
    \Windows\Microsoft.Net\Framework\v2.x\Config
-->


<configuration>
    <appSettings>
  <add key="customsetting1" value="Some text here" />
  <add key="myKey" value=",1,1,1,1,1,1" />
  <add key="RemoveTCPError" value="1,1" />
 </appSettings>
    <connectionStrings>
  <add name="myDatabaseName" connectionString="whatever" />
 </connectionStrings>
    <system.web>
        <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->
        <compilation debug="true"/>
        <!--
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
        -->
        <authentication mode="Windows"/>

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>

    </system.web>
</configuration>

または、web.config ファイル内の任意の場所にコメントネットを追加するか、コメント部分にテキストを追加する必要があります。
それは可能ですか?
私を助けてください。

事前に皆さんに感謝します。

よろしく、
ランジート・ワジェ

解決策 1

web.config ファイル内の任意の場所にコメントネットを追加するか、コメント部分にテキストを追加する必要があります。
それは可能ですか?

可能です、はい! しかし、私はそれを行うことをまったくお勧めしません。

Web.Config は、Web サイトの実行に基づいた構成ファイルです。 誰でも (管理者も) 構成ファイルの編集/更新を許可するには、アプリケーション プールを再起動する必要があります。これは、誰かが別の場所で同時にアプリに接続している場合に問題になります。 さらに、構成ファイルに「コメント」を追加しても、重要な機能や必要な機能とは思えません。

そうでないと思われる場合は、構成ファイルを検索/配置し、ノードを見つけて通常の XML と同様に更新してください。 ファイルを編集/更新する権限があることを確認してください。

解決策 2

を見てください。 WebConfigurationManager クラス[^]。 このクラスは、別のクラスへのアクセスを提供します。 クラス[^]、 Configuration これにより、構成ファイルの作業と編集が可能になります。

コメント

タイトルとURLをコピーしました