如何在 Asp.Net 中读取自定义 .config 文件?

发布于 2024-11-15 07:31:11 字数 1094 浏览 4 评论 0原文

我的 asp.net 网站中有一个自定义配置文件,名为 urls.config,其中包含简单的键值,仅用于重定向目的,现在我想以编程方式读取此文件并在此添加值文件中,我可以使用 XMLTextReader 和 XMLDocument 从此文件中读取值,但无法在此文件中添加值。

任何帮助将不胜感激。 这是我的配置文件结构:

<rewriteMaps>  
  <rewriteMap name="StaticRewrites" />  
  <add key="/superstars4012" value="/article.aspx?articleid=4012" />  
  <add key="/superstars4013" value="/article.aspx?articleid=4013" />  
  <add key="/superstars4014" value="/article.aspx?articleid=4014" />  
  <add key="/superstar" value="/article.aspx?articleid=4012" />  
</rewriteMaps>  

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("urls.config"));
XmlElement 元素 = doc.CreateElement("add");
element.SetAttribute("key", txtAddVanity.Text);
element.SetAttribute("value", "/article.aspx?articleid=4012");
doc.DocumentElement.AppendChild(元素);
doc.Save(Server.MapPath("urls.config"));

如果文件扩展名是 .xml,则效果很好,但当我将其更改为 .config 时,效果不佳,我的要求是 .config,因为重定向在 .xml 中不起作用

I have a custom configuration file in my asp.net website, named urls.config, which contains simple key value, just for redirection purpose, now I want to read this file programmatically and also add values in this file, I am able to use XMLTextReader and XMLDocument to read value from this file, but i am unable to add values in this file.

Any help will be greatly appreciated.
Here is my structure for the configuration file:

<rewriteMaps>  
  <rewriteMap name="StaticRewrites" />  
  <add key="/superstars4012" value="/article.aspx?articleid=4012" />  
  <add key="/superstars4013" value="/article.aspx?articleid=4013" />  
  <add key="/superstars4014" value="/article.aspx?articleid=4014" />  
  <add key="/superstar" value="/article.aspx?articleid=4012" />  
</rewriteMaps>  


XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("urls.config"));
XmlElement element = doc.CreateElement("add");
element.SetAttribute("key", txtAddVanity.Text);
element.SetAttribute("value", "/article.aspx?articleid=4012");
doc.DocumentElement.AppendChild(element);
doc.Save(Server.MapPath("urls.config"));

this works well if the file extension is .xml, but does not when i change it to .config, my requirement is .config, as redirects does not work in .xml

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2024-11-22 07:31:11

正如msdn<中雄辩地指出的那样/a> post,在应用程序运行时修改 web.config 文件当然是可能的(毕竟它只是文本),但这将是一个非常糟糕的主意,因为修改配置文件将导致应用程序在服务器上重新启动 - 结束与当前用户的任何会话,并且所有新访问者都会看到错误页面,直到应用程序重新启动。当然不理想。

从代码的外观来看,

<add key="/superstar" value="/article.aspx?articleid=4012" />  

看来您正在进行从静态 url 到动态查询字符串的 url 重写。有很多更好的方法可以实现这一点。 其中之一是 IIS 模块“用户友好的 URL”。我很少遇到使用这个模块无法解决的问题。

其逻辑如下:

  1. 用户输入“http://www.domain.com/page-this-or-that
  2. url
  3. IIS 有一个正则表达式规则匹配该 url 模式并捕获用户定向到的 http://www.domain.com/page?id=page-this-or-that< /a>
  4. 您收到查询字符串并将其存储在字符串
  5. 中然后执行一个数据库调用,检索并显示标题为“page-this-or-that”的数据库行,

这将更容易维护,并且对您的应用程序和用户来说更安全

- 干杯!

As eloquently stated in this msdn post, modifying the web.config file while the application is running is certainly possible (as it is just text, after all) but would be a very bad idea, in that modifying the config file will result in the application being restarted on the server -- ending any sessions with current users, and all new visitors seeing an error page until the application is restarted. Certainly not ideal.

From the look of your code,

<add key="/superstar" value="/article.aspx?articleid=4012" />  

It appears you are doing url rewriting from a static url to a dynamic query string. There are much better ways of accomplishing this. One is the IIS module "user-friendly urls." I have very few problems that I couldnt solve using this module.

Its logic is as follows:

  1. user enters "http://www.domain.com/page-this-or-that
  2. IIS has a regex rule that matches that url pattern and catches the url
  3. the user is directed to http://www.domain.com/page?id=page-this-or-that
  4. You recieve the querystring and store it in a string
  5. You then execute a database call that retrieves and displays the database row with the title "page-this-or-that"

This would be much easier to maintain, as well as safer for your application and your users.

-Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文