Comment ajouter un commentaire dans le fichier web.config lors de l’exécution dans asp.net

la programmation


Chers tous,

Bonjour, je souhaite ajouter quelques commentaires dans mon fichier web.config au moment de l’exécution.
Moyens par exemple

Le fichier Web.config d’origine est :

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>

Sur un clic de bouton, après l’avoir modifié comme :

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>

ou n’importe où dans le fichier web.config, des réseaux de communication doivent être ajoutés ou dans toute partie commentée, du texte doit être ajouté.
Est-ce possible.
S’il vous plaît aidez-moi.

Merci à tous d’avance.

Salutations,
Ranjeet Waje

Solution 1

n’importe où dans le fichier web.config, des réseaux de communication doivent être ajoutés ou dans toute partie commentée, du texte doit être ajouté.
Est-ce possible.

Possible, oui ! Mais je ne suggérerais pas du tout de le faire.

Web.Config est un fichier de configuration basé sur lequel votre site Web s’exécute. Permettre à quiconque (même à l’administrateur) de modifier/mettre à jour le fichier de configuration nécessiterait un redémarrage de votre pool d’applications, ce qui poserait un problème si quelqu’un est connecté à votre application en même temps ailleurs. De plus, l’ajout d’un “commentaire” au fichier de configuration ne semble nulle part une fonctionnalité importante ou nécessaire.

Si vous pensez autrement, recherchez/localisez simplement le fichier de configuration, recherchez le nœud et mettez-le à jour comme un XML normal. Assurez-vous que vous êtes autorisé à modifier/mettre à jour le fichier.

Solution 2

Jetez un oeil à WebConfigurationManager classe[^]. Cette classe donne à son tour accès à une autre classe[^], Configuration qui vous permettra de travailler et d’éditer les fichiers de configuration.

コメント

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