如何获得配置文件web.config中的信息啊?

gz116 2009-09-21 04:06:51
我的项目中用到了伪静态,因此必须在配置文件中添加了如下的信息

<?xml version="1.0"?>
<configuration>
<configSections>

<section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>
............省略.........
</configSections>
............省略.........

<rewriter>

<rewrite url="([a-zA-Z0-9]+)_Index.html$" to="~/System/article_index.aspx?typeid=$1" processing="stop"/>
<rewrite url="([a-zA-Z0-9]+)/Index.html$" to="~/System/article_index.aspx?typeid=$1" processing="stop"/>
</rewriter>
</configuration>



我想获得 rewrite中的url的值和to的值,怎么获得呢?
...全文
199 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
gz116 2009-09-26
  • 打赏
  • 举报
回复
没人了吗?...
zuoming120 2009-09-22
  • 打赏
  • 举报
回复
chen_ya_ping 2009-09-22
  • 打赏
  • 举报
回复
xml的操作,很多网上也有。
string path = Server.MapPath("web.config");

IEnumerable<XElement> results = XDocument.Load(path).Elements(XName.Get("configuration"));
foreach (XElement result in results)
{
IEnumerable<XElement> querys= result.Descendants(XName.Get("rewrite"));
foreach (XElement query in querys)
{
if (query.Name == "rewrite")
{
Response.Write(query.Attribute("url")+"<br/>");
}
}
}
shalen520 2009-09-22
  • 打赏
  • 举报
回复
写一个类,继承ConfigurationSection,直接读取属性
爪哇鹅 2009-09-22
  • 打赏
  • 举报
回复
ConfigurationManager.GetSection("rewriter"),这是一个Object类型,还得转装成你的rewriter类型,之后不知道能不能读取子项的URL
shalen520 2009-09-22
  • 打赏
  • 举报
回复
.Net framework提供了强大的配置支持,为啥舍进而求远,直接读取xml?
chen_ya_ping 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 gz116 的回复:]
引用 6 楼 chen_ya_ping 的回复:
xml的操作,很多网上也有。
string path = Server.MapPath("web.config");

        IEnumerable <XElement> results = XDocument.Load(path).Elements(XName.Get("configuration"));
        foreach (XElement result in results)
        {
          IEnumerable <XElement> querys= result.Descendants(XName.Get("rewrite"));
          foreach (XElement query in querys)
          {
              if (query.Name == "rewrite")
              {
                  Response.Write(query.Attribute("url")+" <br/>");
              }
          }
        }

XElement 用到命名空间System.Xml.Linq
VS2005中没有啊,不知道怎么办....

[/Quote]
那你可以去CNBLOGS找的xml如何操作。有很多的资料
gz116 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 chen_ya_ping 的回复:]
xml的操作,很多网上也有。
string path = Server.MapPath("web.config");

        IEnumerable <XElement> results = XDocument.Load(path).Elements(XName.Get("configuration"));
        foreach (XElement result in results)
        {
          IEnumerable <XElement> querys= result.Descendants(XName.Get("rewrite"));
          foreach (XElement query in querys)
          {
              if (query.Name == "rewrite")
              {
                  Response.Write(query.Attribute("url")+" <br/>");
              }
          }
        }
[/Quote]
XElement 用到命名空间System.Xml.Linq
VS2005中没有啊,不知道怎么办....
gz116 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
IDictionary  ID=(IDictionary)ConfigurationSettings.GetConfig("rewriter"); 
string  str  =  (string)ID["url"]  +"  "+(string)ID["to"]; 

[/Quote]
提示错误

无法将类型为“System.Configuration.ConfigXmlElement”的对象强制转换为类型“System.Collections.IDictionary”。
dotnetsong 2009-09-21
  • 打赏
  • 举报
回复
用configManager来读取吧
程序漫步 2009-09-21
  • 打赏
  • 举报
回复
用XPATH 就可以。。当XML 读
wuyq11 2009-09-21
  • 打赏
  • 举报
回复
IDictionary ID=(IDictionary)ConfigurationSettings.GetConfig("rewriter");
string str = (string)ID["url"] +" "+(string)ID["to"];
gz116 2009-09-21
  • 打赏
  • 举报
回复
各位大哥大姐帮帮忙啊

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧