从代码 C# 更改 Web.Config 中的 URL 重写规则 [英] Change URL Rewrite Rule in Web.Config from Code C#
问题描述
我想修改 C# 代码的重写规则.Url Rewrite 规则位于 web.config 文件中.
I want to modify rewrite rule from C# code. Url Rewrite rule is resides in web.config file.
<system.webServer>
<rewrite>
<rules>
<rule name="partners">
<match url="^partners$" />
<action type="Rewrite"
url="partners.aspx" />
</rule>
<rule name="news">
<match url="^news$" />
<action type="Rewrite"
url="news.aspx" />
</rule>
<rule name="projects">
<match url="^projects$" />
<action type="Rewrite"
url="projects.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
我想换个ex.<代码><规则名称=合作伙伴"><match url="^partners$"/> 到
,
I want to change for ex. <rule name="partners"> <match url="^partners$" />
to <rule name="partners"> <match url="^friendship/partners$" />
,
如何找到节点规则并将匹配 url 更新为new one" where name = "partners";?
how can I find node rule and update match url to "new one" where name = "partners";?
这是我对动态 url 重写的想法.如果你有任何其他方式,谢谢.
this is my idea for dynamic url rewriting. thanks for any other ways if you have.
推荐答案
我使用以下代码更改了 web.config 网站中 connectionString 的值:
I change value for connectionString in my web.config website with this code :
也许这个例子可以帮助你(只需改变 system.webServer
的 connectionString
值和 rules
的 add
代码>等.请告诉我它是否适合您
May be this example can help you (just change the value connectionString
by system.webServer
and add
by rules
etc..
Please tell me if it works for you
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load("../myPath/web.config");
foreach (XmlNode node in myXmlDocument["configuration"]["connectionStrings"])
{
if (node.Name == "add")
{
if (node.Attributes.GetNamedItem("name").Value == "SCI2ConnectionString")
{
node.Attributes.GetNamedItem("connectionString").Value = "new value";
}
}
}
这篇关于从代码 C# 更改 Web.Config 中的 URL 重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!