当前位置: 首页 > 编程语言 > C#

如何获取web配置location元素?分享

时间:2023-04-10 15:32:32 C#

web配置location元素如何获取?如何获取webconfig位置元素?ConfigurationManager.GetSection("appSettings")返回OkayConfigurationManager.GetSection("location")返回nullIE......您收到错误的原因是因为在.NET中,自定义应用程序配置部分(例如“Location"节)要求您提供自定义配置节处理程序。您需要使用的主要接口是IConfigurationSectionHandler这是一篇关于如何创建自定义配置处理程序的MSDN文章。这有帮助吗?配置config=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);ConfigurationLocationCollectionmyLocationCollection=config.Locations;foreach(ConfigurationLocationmyLocationinmyLocationCollection){Console.WriteLine("LocationPath:{0}",myLocation.Path);打开配置();Console.WriteLine("位置配置文件路径:{0}",myLocationConfiguration.FilePath);}Console.WriteLine("完成...");控制台.ReadLine();从这里不确定这是否正是您想要的,但您可以像这样获取web.config位置元素中的部分......"文件夹/Page2.aspx");我想改进Neil的回答:许多人说不建议在运行时修改web.config。但代码是这样做的。//web.config文件所在路径stringpath="~/Administrator/";//用逗号分隔的aspx页面名称的集合。//文本框中的示例内容:Default.aspx,Audit.aspx,stringstrPages=txtPages.Text;//这是字符串数组,我们将在其中分解所有aspx页面的名称//包含在strPages变量string[]cdrPages=strValues.Split(',');//这是我们要传输我们的aspx页面名称的列表//以更快地搜索现有项目Listaccesslist=newList();尝试{//1。创建角色System.Web.Security.Roles.CreateRole(this.txtRoleName.Text);//2。打开Web配置-->确保将web.config文件的正确文件夹位置放入System.Configuration.Configurationconfig=System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(path);//3。获取所有指定位置ConfigurationLocationCollectionmyLocationCollection=config.Locations;//4。将string[]strPages的值传递给ListaccessListfor(inti=0;iaccessListboolexists=accesslist.Exists(element=>element==myLocation.Path);//IfExistsif(exists){//7.打开myLocation的配置System.Configuration.Configurationsub=myLocation.OpenConfiguration();//8.获取具体位置的授权部分AuthorizationSectionsection=(System.Web.Configuration.AuthorizationSection)sub.GetSection("system.web/authorization");//9.声明授权规则,在这种情况下,我们允许新角色访问特定页面AuthorizationRuleautho=newSystem.Web.Configuration.AuthorizationRule(System.Web.Configuration.AuthorizationRuleAction.Allow);//10.将新角色添加到授权部分autho.Roles.Add(this.txtRoleName.Text);section.Rules.Add(autho);//11.保存“子”或web.config文件里面的具体位置。sub.Save();}}message.InnerHtml="RoleSuc成功添加!”;message.Attributes.Add("class","msg_info");}catch{message.InnerHtml="SavingFailed";message.Attributes.Add("class","msg_error");}这可能是一个丑陋的代码,但肯定它会工作——JanRussel'RustyProgrammer'Clachan这是因为appSettings是.NET应用程序中已知的(默认)配置部分。如果你想使用你自己的配置部分,你必须创建它上面是C#学习教程:如何获取web配置location元素?全部内容分享,如果对你有用,需要详细了解C#学习教程,希望大家多多关注。这篇文章是收集自网络,不代表立场,涉及侵权请点击右联系管理员删除,如需转载请注明出处: