动态添加删除web.config节点

 

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值