asp.net2.0 中 web.config 的一些改變

來源:互聯網
上載者:User
          

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ShowMessage(string temp)
    {
        this.Response.Write("<script>alert('" + temp + "')</script>");
    }
    //擷取 appsetting 的值
    protected void Get_AppSetting_Click(object sender, EventArgs e)
    {  
        string message=System .Web.Configuration.WebConfigurationManager.AppSettings["message"];

        this.ShowMessage(message);

    }
    //取得聯結字串的值
    protected void Get_Connection_Click(object sender, EventArgs e)
    {
        string cnn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["cnn"].ConnectionString  ;
        this.ShowMessage(cnn);

    } 

    //用 xpath 方式擷取config 中的一section
    protected void Get_Section_Click(object sender, EventArgs e)
    {
        Configuration config;

        config = WebConfigurationManager.OpenWebConfiguration("~");

        System.Web.Configuration.IdentitySection section;
        section =config.GetSection("system.web/identity") as System.Web.Configuration.IdentitySection;
        if (section != null)
        {
          ShowMessage(section.Impersonate.ToString () );  
        }
  


    }
    //改變結點資訊
    protected void Change_Section_Click(object sender, EventArgs e)
    {
        Configuration config;
        config = WebConfigurationManager.OpenMachineConfiguration("~");
        System.Web.Configuration.IdentitySection section;
        section = config.GetSection("system.web/identity") as System.Web.Configuration.IdentitySection;
        if (section != null)
        {
            section.Impersonate = !section.Impersonate ;
            config.Save();

            ShowMessage("debug setting is now " + section.Impersonate.ToString());

        }

    }

    //擷取外部資料
    protected void Get_External_Click(object sender, EventArgs e)
    {
        string cnn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
        this.ShowMessage(cnn);
    }
    
}

cnn.config<connectionStrings>
    <add name ="cnn" connectionString ="cnnvalue"/>
    
</connectionStrings>

web.config <?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 xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    
    
    
    <appSettings>
        <add key="message" value="hello world"/>
    </appSettings>
    
    
    <connectionStrings configSource ="cnn.config">
        
    
    </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"/>
        <identity impersonate="true"/>
        
    
        <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.

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

具體可以看這....
http://odetocode.com/Articles/418.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.