Cách thêm Nhận xét vào tệp web.config khi chạy trong asp.net

lập trình


Kính thưa tất cả,

Xin chào, tôi muốn thêm một số nhận xét vào tệp web.config của mình khi chạy.
Nghĩa là chẳng hạn

tệp Web.config gốc là:

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>

Trên một số nút bấm, sau khi nó được sửa đổi như :

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>

hoặc bất kỳ vị trí nào trong tệp web.config nên thêm commnet hoặc trong bất kỳ văn bản phần nhận xét nào nên được thêm vào.
Điều này có thể được không?
Làm ơn giúp tôi.

Cảm ơn tất cả trước.

Trân trọng,
Ranjeet Waje

Giải pháp 1

nên thêm bất kỳ vị trí nào trong tệp web.config commnet hoặc trong bất kỳ văn bản phần nhận xét nào nên được thêm vào.
Điều này có thể được không?

Có thể, Có! Nhưng tôi sẽ không đề nghị làm điều đó chút nào.

Web.Config là tệp cấu hình dựa trên đó trang web của bạn đang chạy. Việc cho phép bất kỳ ai (kể cả quản trị viên) chỉnh sửa/cập nhật tệp cấu hình sẽ cần khởi động lại nhóm ứng dụng của bạn, điều này sẽ là một vấn đề nếu ai đó được kết nối với ứng dụng của bạn cùng lúc ở một nơi khác. Hơn nữa, việc thêm “nhận xét” vào tệp cấu hình sẽ không tạo ra tính năng quan trọng hoặc cần thiết.

Nếu bạn nghĩ khác, chỉ cần tìm/định vị tệp cấu hình, tìm nút và cập nhật nó giống như một XML thông thường. Hãy đảm bảo rằng bạn có quyền chỉnh sửa/cập nhật tệp.

Giải pháp 2

Hãy nhìn vào WebConfigurationManager lớp học[^]. Lớp này lần lượt cung cấp quyền truy cập vào lớp khác lớp học[^], Configuration điều này sẽ cho phép bạn làm việc và chỉnh sửa các tập tin cấu hình.

コメント

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