如何在asp.net运行时在web.config文件中添加注释


各位,

您好,我想在运行时在 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 文件中的任何位置添加 commnets,或者在任何注释部分添加文本。
这可能吗。
请帮我。

提前感谢大家。

问候,
兰吉特·瓦杰

解决方案1

应在 web.config 文件中的任何位置添加 commnets,或在任何注释部分添加文本。
这可能吗。

可能,是的! 但我根本不建议这样做。

Web.Config 是您的网站运行所基于的配置文件。 允许任何人(甚至管理员)编辑/更新配置文件将需要重新启动应用程序池,如果有人在其他地方同时连接到您的应用程序,这将是一个问题。 此外,向配置文件添加“注释”听起来并不重要或必要的功能。

如果您不这么认为,只需查找/定位配置文件,然后找到节点并像普通 XML 一样更新它。 请确保您有权编辑/更新该文件。

解决方案2

看看 WebConfigurationManager 班级[^]。 这个类又提供对另一个类的访问 班级[^], Configuration 这将允许您工作和编辑配置文件。

コメント

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