如何在 Asp.Net 中读取自定义 .config 文件?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如msdn<中雄辩地指出的那样/a> post,在应用程序运行时修改 web.config 文件当然是可能的(毕竟它只是文本),但这将是一个非常糟糕的主意,因为修改配置文件将导致应用程序在服务器上重新启动 - 结束与当前用户的任何会话,并且所有新访问者都会看到错误页面,直到应用程序重新启动。当然不理想。
从代码的外观来看,
看来您正在进行从静态 url 到动态查询字符串的 url 重写。有很多更好的方法可以实现这一点。 其中之一是 IIS 模块“用户友好的 URL”。我很少遇到使用这个模块无法解决的问题。
其逻辑如下:
这将更容易维护,并且对您的应用程序和用户来说更安全
- 干杯!
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,
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:
This would be much easier to maintain, as well as safer for your application and your users.
-Cheers!