using System.Web;
using System.Web.Configuration;
using System.Configuration;
using System;namespace Web练习
{
public class EditWebConfigNodes
{
public bool ProtectedWebConfig(string sectionName)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
string provider = "RsaProtectedConfigurationProvider";
if (!config.GetSection(sectionName).SectionInformation.IsProtected)
{
config.GetSection(sectionName).SectionInformation.ProtectSection(provider);
try
{
config.Save();
return true;
}
catch (Exception)
{
return false;
}
}
else
{
return true;
}
}
public bool ProtectedWebConfig(string sectionName, string path)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
string provider = "RsaProtectedConfigurationProvider";
if (!config.GetSection(sectionName).SectionInformation.IsProtected)
{
config.GetSection(sectionName).SectionInformation.ProtectSection(provider);try
{
config.Save();
return true;
}
catch (Exception)
{
return false;
}
}
else
{
return true;
}
}
public bool UnProtectedWebConfig(string sectionName)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
if (config.GetSection(sectionName).SectionInformation.IsProtected)
{
config.GetSection(sectionName).SectionInformation.UnprotectSection();
try
{
config.Save();
return true;
}
catch (Exception)
{
return false;
}
}
else
{
return true;
}
}
public bool UnProtectedWebConfig(string sectionName, string path)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
if (config.GetSection(sectionName).SectionInformation.IsProtected)
{
config.GetSection(sectionName).SectionInformation.UnprotectSection();
try
{
config.Save();
return true;
}
catch (Exception)
{
return false;
}
}
else
{
return true;
}
}
public bool AddWebConfigNode(string path,string verb,string handleClass)
{
string name = @"system.web/httpHandlers"; //配置节节点
string appPath = @"/";//虚拟路径
Configuration config = WebConfigurationManager.OpenWebConfiguration(appPath);//打开配置节
HttpHandlersSection section = (HttpHandlersSection)config.GetSection(name);//获取system.web/httpHandlers配置节
//定义子节点 *.aaa表示请求后缀是.aaa,第二个参数表示使用System.Web.HttpForbiddenHandle来处理前面的.aaa请求,第三个参数是类型[get,put,post],*表示所有
HttpHandlerAction newHandle = new HttpHandlerAction(path, handleClass, verb);
section.Handlers.Add(newHandle);
try
{
config.Save();
return true;
}
catch (Exception)
{
return false;
}
}
public bool DelWebConfigNode(string verb, string path)
{string name = @"system.web/httpHandlers"; //配置节节点
string appPath = @"/";//虚拟路径
Configuration config = WebConfigurationManager.OpenWebConfiguration(appPath);//打开配置节
HttpHandlersSection section = (HttpHandlersSection)config.GetSection(name);//获取system.web/httpHandlers配置节
//定义子节点 *.aaa表示请求后缀是.aaa,第二个参数表示使用System.Web.HttpForbiddenHandle来处理前面的.aaa请求,第三个参数是类型[get,put,post],*表示所有
section.Handlers.Remove(verb, path);
try
{
config.Save();
return true;
}
catch (Exception)
{
return false;
}
}
}
}
2009-11-04-13:41:44