web.config
时间:2010-09-28 来源:eolande
設置web.config
<?xml version="1.0" ?>
<configuration>
<appSettings>
<add key="websiteName" value="My New Website"/>
<add key="welcomeMessage" value="Welcome to my new Website, friend!"/>
</appSettings>
<system.web>...</system.web>
</configuration>
取得<appSettings>的值
using System.Web.Configuration;
protected void Page_Load(object sender, EventArgs e)
{
lblSiteName.Text =
WebConfigurationManager.AppSettings["websiteName"];
lblWelcome.Text =
WebConfigurationManager.AppSettings["welcomeMessage"];
}
取得web.config其它节点的值---使用WebConfigurationManage.GetSection(“…”);
using System.Web.Configuration;
…
CompilationSection compSection =
(CompilationSection)WebConfigurationManager.GetSection("system.web/compilation");
foreach (AssemblyInfo assm in compSection.Assemblies)
{
Label2.Text+=assm.Assembly + "<br />";
}
写入web.config
using System.Web.Configuration;
using System.Configuration;
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);//取得Configuration 对象
config.AppSettings.Settings["welcomeMessage"].Value = "Welcome, again.";
config.Save();
Request.ApplicationPath
Request.CurrentExecutionFilePath
custom section
Creating a Section Class
for excample:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
// 定义继承自System.Configuration.ConfigurationSection的public类
//注意不要将此类放入任何namespace中。
public class OrderService : ConfigurationSection {
//It’s Called:strongly typed properties
[ConfigurationProperty("available",
IsRequired = false, DefaultValue = true)]
public bool Available
{
get { return (bool)base["available"]; }
set { base["available"] = value; }
}
[ConfigurationProperty("pollTimeout",
IsRequired = true)]
public TimeSpan PollTimeout
{
get { return (TimeSpan)base["pollTimeout"]; }
set { base["pollTimeout"] = value; }
}
[ConfigurationProperty("location",
IsRequired = true)]
public string Location
{
get { return (string)base["location"]; }
set { base["location"] = value; }
}
}
Registering a Section Class
<configSections>
<section name="orderService" type="OrderService"/>
</configSections>
<orderService available="true"
pollTimeout="00:01:00"
location="tcp://OrderComputer:8010/OrderService"/>
Retrieve the information from your custom section
OrderService custSection =
(OrderService)ConfigurationManager.GetSection("orderService");
lblInfo.Text += "Retrieved service information...<br />" +
"<b>Location:</b> " + custSection.Location +
"<br /><b>Available:</b> " + custSection.Available.ToString() +
"<br /><b>Timeout:</b> " + custSection.PollTimeout.ToString() + "<br /><br />";
显示结果:
LabelRetrieved service information...
Location: tcp://OrderComputer:8010/OrderService
Available: True
Timeout: 00:01:00
Programmatic Encryption
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
ConfigurationSection appSettings = config.GetSection("appSettings");
if (appSettings.SectionInformation.IsProtected)
{
appSettings.SectionInformation.UnprotectSection();
}
else
{
appSettings.SectionInformation.ProtectSection(
"DataProtectionConfigurationProvider");
}
config.Save();
Command-Line Encryption
• The -pe switch specifies the configuration section to encrypt.
• The -app switch specifies your web application’s virtual path.
• The -prov switch specifies the provider name.
aspnet_regiis.exe command-line utility
c:\Windows\Microsoft.NET\Framework\[Version]
h t t p://localhost/TestApp:
aspnet_regiis -pe "appSettings" -app "/TestApp"
-prov "DataProtectionConfigurationProvider"